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

The system cannot find the file specified but file exists

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

我可以在我的文件夹中看到该文件并且可以打开它。 代码:

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();            
domFactory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("MyFolder\Test.xml"));

我收到这个错误:

java.io.FileNotFoundException: C:\MyFolder\Test.xml (The system cannot find the file specified)

为什么代码 open/read 我的文件不能,但其他程序如 Notepad++ 可以这样做?

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

看起来您正在使用下面提到的行中的相对路径来访问该文件。

Document doc = builder.parse(new File("MyFolder\Test.xml"));

此外,您使用了单个“\”,而不是“//”。您可以使用两个选项进行调试

  1. 尝试使用文件的绝对路径(始终使用“//”)并查看应用程序是否有权访问该文件。如果可以访问,则从程序执行的目录形成正确的相对路径。

  2. 如果由于某种原因您的程序无法访问具有访问权限的文件,请尝试提供所需的权限。

我最近遇到了同样的问题,我发现在我的情况下文件被保存为 "abc.txt.txt" 而不是 "abc.txt"。

由于文件的扩展名被隐藏了,我之前看不到文件是在名称中添加了扩展名的。

检查文件是否以正确的扩展名保存。

或者,由于您的文件名中包含 "date",这可能会导致问题。检查访问文件时的日期格式是否与文件名中的日期格式相同。

试试怎么样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},也许您的日期格式类似于 0618?

.\ 添加到文件名的开头。

.代表当前运行目录

看起来 java 正在尝试从根文件夹 (C:\) 中查找文件夹。添加 . 告诉 java 查看当前 运行 目录,而不是 C:\。更好的是,不要使用反斜杠并使用单个正斜杠:

new File("./MyFolder/Test.xml")

试试这个:

    System.out.println(new File("").getAbsolutePath());

它将在控制台日志中写入您当前的工作目录。然后你可以像这样调整你的代码:

    System.out.println(new File("relative/path/to/File.xml").exists);

它应该告诉你文件(或目录)是否存在。注意是“/”而不是“\”。

请将您的代码修改为:

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 个文件构建项目。 ClassLoader 需要有 .class 个文件。否则,它将无法从类路径中读取文件夹或文件。

注:

  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)

  • 我试图解析其他文件夹中的 xml 文件。打印成功了。代码如下

  • 如果需要绝对路径,也可以使用Classloader加载XML文件

    import java.io.File;
    import java.io.IOException;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    public class Testing {
    
        public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
            parseXmlFile();
        }
    
        private static void parseXmlFile() throws ParserConfigurationException, SAXException, IOException {
            DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setIgnoringComments(true);
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("src/main/java/xmlfiles/Test.xml"));
            if (doc.hasChildNodes()) {
                printNote(doc.getChildNodes());
    
            }
        }
    
        private static void 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());
                }
    
            }
        }
    }
    

<?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>