首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >系统找不到指定的文件,但文件存在。

系统找不到指定的文件,但文件存在。
EN

Stack Overflow用户
提问于 2019-02-28 16:00:02
回答 7查看 2.7K关注 0票数 2

我正在尝试操作名为Test.XML的XML文件。

我可以看到文件夹中的文件,也可以打开它。代码:

代码语言:javascript
复制
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();            
domFactory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("MyFolder\Test.xml"));

我得到了这个错误:

代码语言:javascript
复制
java.io.FileNotFoundException: C:\MyFolder\Test.xml (The system cannot find the file specified)

为什么代码不能打开/读取我的文件,但是像Notepad++这样的其他程序可以这样做?

*注意:文件的真实名称是"Use-cases\testSuitesA_E_1002+${user}3_12022016+${date}2_2.5.xml".

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2019-03-07 06:58:12

请修改您的代码如下:

代码语言:javascript
复制
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File(classLoader.getResource("MyFolder/Test.xml").getPath()));
System.out.println(doc.getDocumentElement());

要运行此代码,请为.class 文件构建项目。.class ClassLoader需要有文件。否则,它将无法从类路径.读取文件夹或文件。

注:

  1. 新文件(“MyFolder\Test.xml”)- This will not work because you have not provided the absolute path. You have to use classloader to get file from classpath (in that case, you don't have to mention the full path). Classloader brings the full absolute path for you. Remember : java.nio.File needs absolute path for its working.
  2. If you want to read file from any arbitrary location, then you have to specify the full path for that.(assuming that you are trying to access the file outside)
票数 3
EN

Stack Overflow用户

发布于 2019-03-04 12:18:55

试试Document doc = builder.parse(new File("Use-cases\\testSuitesA_E_1002+${user}3_12022016+${date}2_2.5.xml"))怎么样?

在您的文件路径中,用例\testSuitesA_E_1002+${user}3_12022016+${date}2_2.5.xml \t表示转义序列。

另外,我想检查一下您正在使用的{date},也许您的约会被格式化为06\06\2018?

票数 1
EN

Stack Overflow用户

发布于 2019-03-07 16:39:54

  • 我试图解析其他文件夹中的xml文件。印得很好。代码如下
  • 如果需要绝对路径,还可以使用Classloader加载XML文件。 导入(java.io.File);导入(java.io.IOException);导入(javax.xml.parsers.DocumentBuilder);导入(javax.xml.parsers.DocumentBuilderFactory);导入(org.w3c.dom.Document);导入(org.w3c.dom.Node);导入(org.xml.sax.SAXException);公共类测试(公共静态空洞(String[]))抛出ParserConfigurationException,SAXException,IOException ();}私有静态parseXmlFile()抛出ParserConfigurationException,SAXException,IOException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();domFactory.setIgnoringComments(真);DocumentBuilder builder = domFactory.newDocumentBuilder();Document =builder.parse(新文件(“src/main/java/xmlfiles/Test.xml”));if (doc.hasChildNodes()) { printNote(doc.getChildNodes());}}私有静态无效printNote(NodeList nodeList) { for (int count = 0;count < nodeList.getLength();count++) { Node tempNode = nodeList.item(count);if (tempNode.getNodeType() == Node.ELEMENT_NODE) { System.out.println("Value =“+tempNode.getTextContent();})

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<company>
	<staff id="100">
		<firstname>Tom</firstname>
		<lastname>Jerry</lastname>
		<nickname>Tomy</nickname>
		<salary>100000</salary>
	</staff>
	<staff id="200">
		<firstname>Micky</firstname>
		<lastname>Mouse</lastname>
		<nickname>Mike</nickname>
		<salary>200000</salary>
	</staff>
</company>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54929686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档