我正在用Java编写一个程序,我从XML文件中读取数据并解析它。该文件被导入到我的项目的src目录中名为“Resources”的文件夹中。我正在使用Eclipse。当我运行该程序,我得到以下错误:FileNotFoundException在读取.xml文件进行解析时
java.io.FileNotFoundException: /Users/thechiman/Dropbox/introcs/PSU SOC Crawler/resources/majors_xml_db.xml (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
...
相关的代码是在这里:
private void parseXML() {
//Get a factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//Use factory to get a new DocumentBuilder
DocumentBuilder db = dbf.newDocumentBuilder();
//Parse the XML file, get DOM representation
dom = db.parse("resources/majors_xml_db.xml");
} catch(ParserConfigurationException pce) {
pce.printStackTrace();
} catch(SAXException se) {
se.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
我不明白为什么我得到FileNotFoundException异常时,该文件是存在的。谢谢您的帮助。
这可能不是问题,但我想我会注意到,在您的描述中,您称之为“资源”文件夹,在代码中它是“资源”。在具有区分大小写路径的操作系统上,这可能会导致问题。 – Ash 2010-04-04 22:22:49
大写是一个错字。这是两个地方的“资源”。 – ericso 2010-04-04 23:08:25
您是否尝试过使用此指令? System.getProperty(“user.dir”) – 2010-04-05 03:39:05