Parsing XML Truncates File Path
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 tru开发者_C百科ncated 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.
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.
Try to use a complete url with a scheme instead of unc path.
file://xxx.xxx.xxx.xxx/path/to/file.xml
精彩评论