解析XML截断文件路径
问题描述:
我收到与下面的代码FileNotFoundException异常:解析XML截断文件路径
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);
}
这导致FileNotFoundException异常:\路径\为\ FILE.xml。它似乎已将IP地址从路径中截断。我检查过路径名不包含任何空格,如果我在分析之前打印出File对象的路径,则会显示完整路径。有任何想法吗?
我运行Java 1.5_14。
答
尝试改变
File dataFile = new File("\\xx.xxx.xx.xxx\PATH\TO\FILE.xml");
到
File dataFile = new File("\\\\xx.xxx.xx.xxx\\PATH\\TO\\FILE.xml");
记得在Java中,\转义下一个字符...
编辑:假设你得到一个从线FNFE:
Document doc = db.parse(dataFile);
那么就意味着datafile.isFile()
在流逝,所以该文件应该存在。只是为了测试目的,你可能想尝试改变,为:
Document doc = db.parse(dataFile.toURI().toString());
或
Document doc = db.parse(new InputSource(new FileReader(dataFile)));
看看会发生什么。
答
尝试使用一个完整的URL有计划的,而不是UNC路径。
文件://xxx.xxx.xxx.xxx/path/to/file.xml
+1你需要逃避文件分隔符。 – ChadNC 2010-01-20 21:17:06
它抛出FileNotFoundException。我已经更新了这个问题。 – 2010-01-20 21:26:54
@Ben Cool,我已经从我的答案中删除了这个问题;-) – 2010-01-20 21:32:32