开发者

File Not Found Error

I've passed a file to a library, and the library is spitting out a FileNotFound error as shown:

javax.xml.transform.TransformerException: java.io.FileNotFoundException: file:\C:\Users\Oro开发者_如何学JAVAma\workspace\IndividualProject_JINQS\WebContent\WEB-INF\classes\presentationlayer\utility\mappings\jmt\networkModel.xml (The filename, directory name, or volume label syntax is incorrect)

The file is sent calling this method:

 private URI getFileLocation(String fName) throws URISyntaxException {
    return this.getClass().getResource("utility/mappings/jmt/"+ fName).toURI();
 }

So if the file didn't exist I'd be getting a null pointer way before I send the file to the JSIM library.

I'm looking at the the errors message: (The filename, directory name, or volume label syntax is incorrect). Looking at the full path for the file, I can't see any special characters.

Does anyone have any idea what might be causing the error?

EDIT: The method call to the other library requires sending a file:

SolverDispatcher solver = new SolverDispatcher();
File networkModel = new File(getFileLocation("networkModel.xml"));
solver.solve(networkModel);


Does the method you are calling expect a URI or a filename? That is, does it expect that file: at the start?


I might be wrong. I'm just guessing. Usually absolute URL of a file starts with a

file:///

In the exception I see it as

file:\


There is no direct convertion from a Resource to a File. You can try removing the "file:\" part from the path, but it might not work in all the cases, ie. when running as an applet, since you can not open a File object with the security model in place.

The best way to read the Resource contents is to get the InputStream:

private InputStream getFileLocation(String fName) throws URISyntaxException {
    return this.getClass().getResourceAsStream("utility/mappings/jmt/"+ fName);
}

SolverDispatcher solver = new SolverDispatcher();
InputStream networkModelStream = getFileLocation("networkModel.xml");
solver.solve(networkModelStream);


I would try getting some debugging info, maybe adding some prints

    private URI getFileLocation(String fName) throws URISyntaxException {
        URL url = this.getClass().getResource("utility/mappings/jmt/"+ fName);
        System.out.printf("url: %s%n", url);
        return url.toURI();
     }

and

    SolverDispatcher solver = new SolverDispatcher();
    URI uri = start.getFileLocation("networkModel.xml");
    System.out.printf("uri: %s%n", uri);
    File networkModel= new File(uri);
    System.out.printf("file: %s - %s%n", networkModel, networkModel.exists()); 
    solver.solve(networkModel);

The last output (file) should not have the protocol part "file:\".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜