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

时间:2019-02-28 16:00:02

标签: java file

我正在尝试操纵名为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)

为什么代码无法打开/读取我的文件,但是其他程序(例如Notepad ++)可以打开/读取我的文件?

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

7 个答案:

答案 0 :(得分:3)

请为此修改您的代码:

ColorListPreset.not(Color.WHITE)

要运行此代码,请为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()); 文件构建项目。 ClassLoader需要具有.class个文件。否则,它将无法从类路径读取文件夹或文件。

注意:

  1. 新文件(“ MyFolder \ Test.xml”)-.class

  2. 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.

答案 1 :(得分:1)

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

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

此外,我想检查一下您使用的{date},也许您的日期格式设置为06 \ 06 \ 2018?

答案 2 :(得分:1)

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

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

    qrc://

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());
            }

        }
    }
}

答案 3 :(得分:0)

好像您在下面提到的行中使用相对路径来访问文件。

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

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

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

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

答案 4 :(得分:0)

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

由于文件的扩展名被隐藏,因此我以前看不到文件的扩展名已被保存。

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

或者,因为您的文件名中带有“日期”,所以可能引起问题。检查访问文件时的日期格式是否与文件名中的日期格式相同。

答案 5 :(得分:0)

在文件名的开头添加.\\

.代表当前正在运行的目录

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

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

答案 6 :(得分:0)

尝试一下:

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

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

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

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

相关问题