views:

36

answers:

2

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.

A: 

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

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

Yoni
+2  A: 

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.

Paul Wagland
+1 you need to escape the file seperators.
ChadNC
It is throwing a FileNotFoundException. I have updated the question.
Ben Hanzl
@Ben Cool, I have removed that question from my answer ;-)
Paul Wagland
@Paul, both of your suggestions worked. I ended up using the URI. Thanks.
Ben Hanzl