【问题标题】:Java Reading xml file error, file not foundJava读取xml文件错误,找不到文件
【发布时间】:2013-10-04 17:34:05
【问题描述】:

我在读取 xml 文件时遇到以下问题,当使用下面的 dirFile 时,我收到以下错误:

C:\Folder\folder1\folder2\project\resources\xmls\file.xml(系统找不到指定的路径)

并且文件在那里并且提到的路径是正确的。但是如果我使用这个路径,它曾经是 C:\repo\eclipse\testcases\file.xml 的作品。我想在项目中包含 xml。

private static String dirFile = "resources/xmls/";

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(dirFile + file);

我们将不胜感激。

【问题讨论】:

  • 如果错误是C:\Folder\folder1\folder2\project\resources\xmls\file.xml (The system cannot find the path specified),那么你在C:\Folder\folder1\folder2\project\resources\xmls\file.xml没有文件
  • 我还会考虑更合理的文件夹命名约定。
  • 我猜 DocumentBuilder.parse() 方法没有按照您的预期走。我不确定它应该如何精确,但如果你使用绝对路径而不是相对路径,并且如果它有效,那么你应该更改“dirFile”的内容。

标签: java xml eclipse


【解决方案1】:

如果您收到错误:C:\Folder\folder1\folder2\project\resources\xmls\file.xml (The system cannot find the path specified) 这意味着以下两种情况之一:

  1. 该路径上不存在该文件
  2. 您用来定位文件的路径不正确。

您应该通过转到资源管理器中的文件并右键单击该文件来仔细检查绝对路径,然后进入属性。确保所有目录都正确且顺序正确。

【讨论】:

  • 这是绝对路径,我需要添加 src/main 不敢相信我错过了。谢谢。
  • 没问题。很高兴我能帮上忙。
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 2011-12-04
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多