Question

I am receiving a FileNotFoundException with the following code:

File dataFile = new File("\\xx.xxx.xx.xxx\PATH\TO\FILE.xml");

if(dataFile.isFile())
{
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    // Printing out File displays full path
    Document doc = db.parse(dataFile);
}

This is resulting in a FileNotFoundException: \PATH\TO\FILE.xml. It appears to have truncated the IP address out of the path. I have checked that the path name does not include any spaces and if I print out the path of the File object before parsing, the full path is displayed. Any ideas?

I am running Java 1.5_14.

Was it helpful?

Solution

Try changing

File dataFile = new File("\\xx.xxx.xx.xxx\PATH\TO\FILE.xml");

to

File dataFile = new File("\\\\xx.xxx.xx.xxx\\PATH\\TO\\FILE.xml");

remember that in Java, \ escapes the next character...

Edit: Assuming that you are getting a FNFE from the line:

Document doc = db.parse(dataFile);

then it means that the datafile.isFile() is passing, and so the file should exist. Just for testing purposes, you might want to try changing that to:

Document doc = db.parse(dataFile.toURI().toString());

or

Document doc = db.parse(new InputSource(new FileReader(dataFile)));

And see what happens.

OTHER TIPS

Try to use a complete url with a scheme instead of unc path.

file://xxx.xxx.xxx.xxx/path/to/file.xml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top