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

这样的天使:

我想操纵我的所谓的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)

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

***注:文件的真实名称是 “使用情况\ testSuitesA_E_1002 + $ {}用户+ 3_12022016 $ {日期和时间} 2_2.5.xml”。

阿尼什B.:

请修改您的代码如下:

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文件。否则,将无法从classpath中读取文件夹或文件。

注意 :

  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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

该系统找不到指定的文件

詹金斯 | “该系统找不到指定的文件”

Makefile:CreateProcess(...)失败。该系统找不到指定的文件

该系统找不到指定的文件。在Visual Studio中

该系统找不到指定的文件。.p12 证书文件错误

java.io.IOException : ipm.note 。该系统找不到指定的文件

跟踪器:错误TRK0005:找不到:“ CL.exe”。该系统找不到指定的文件

节点gyp错误TRACKER:错误TRK0005:找不到:“ CL.exe”。该系统找不到指定的文件

无法加载文件或程序集或其依赖项之一。该系统找不到指定的文件

无法加载文件或程序集“XXX”或其依赖项之一。该系统找不到指定的文件

无法加载文件或程序集“路径”或其依赖项之一。该系统找不到指定的文件

XamlParseException:无法加载文件或程序集“ ResourceLibrary,...”或其依赖项之一。该系统找不到指定的文件

系统找不到指定的文件

WPF XAML无法加载文件或程序集'AssemblyName,PublicKeyToken = null'或其依赖项之一。该系统找不到指定的文件

无法加载文件或程序集“ Microsoft.Data.Service”或其依赖项之一。该系统找不到指定的文件

操作无法完成。该系统找不到指定的路径

AppStartError: CreateProcess: 系统找不到指定的文件

Android Studio 系统找不到指定的文件

Cassandra-“系统找不到指定的文件”

\ Dockerfile:系统找不到指定的文件

例外-系统找不到指定的文件

系统找不到Java中指定的文件

系统找不到指定的Java文件

CF推送-系统找不到指定的文件

GetFileFromApplicationUriAsync 抛出“系统找不到指定的文件”

系统找不到用ffmpeg指定的文件

系统找不到RStudio中指定的文件

批处理:系统找不到指定的文件

复制函数返回当文件存在时,系统找不到指定的文件