【问题标题】:FileNotFoundException while reading an existing file in Java Maven project在 Java Maven 项目中读取现有文件时出现 FileNotFoundException
【发布时间】:2021-01-10 21:33:16
【问题描述】:

我正在尝试读取 Maven 项目中的现有文件,但它显示 FileNotFoundException。我相信我得到了正确的文件路径,我被困住了,不知道下一步该去哪里。 这是我所拥有的:

  • pom.xml:
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.company</groupId>
  <artifactId>weather</artifactId>
  <version>1.0</version>

  <name>weather</name>
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.release>15</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

我的 Maven 目录树:

  • 我检查路径:
    String fileName = "/weatherstack.json";
    
    String path = getClass().getResource(fileName).getPath();
    
    System.out.println(path);

输出:/Users/.../.../weather/target/classes/weatherstack.json

  • 读取文件:

Reader reader = new FileReader(path);

错误:未处理的异常类型 FileNotFoundException

感谢任何帮助!

【问题讨论】:

  • 您是否尝试过直接进入您目录中的 JSON 文件并复制文件位置?
  • 你试过getClass().getClassLoader().getResource(fileName)吗?
  • @AdrianRusso 嗨,阿德里安!我试图从 JSON 文件中“复制路径”,它似乎与我上面得到的输出相同。你知道会出什么问题吗?谢谢!
  • @f1sh 嗨 f1sh!谢谢你的评论。当我尝试getClass().getClassLoader().getResource(fileName) 时,它返回 null :(
  • 如果从fileName 中删除前导/ 会怎样?

标签: java maven filereader filenotfoundexception


【解决方案1】:

public FileReader​(String fileName) throws FileNotFoundException 表示您必须:

  • try...catch 方法内的 FNFE 或
  • 在封闭方法的标头中声明它。

这样就不再是“未处理”了。

【讨论】:

  • 嗨,杰罗德!感谢您的回答,对于我不清楚的问题,我很抱歉。你是对的,我应该尝试捕捉异常。我想解决的真正问题是我有一个有效的文件,我想阅读它,没有任何异常发生。
  • @AugustVo 请编辑您的 Q 以使其清楚。见How do I ask a good question?
  • 我更新了我的 Q。希望现在更清楚了。感谢您的耐心:)
  • 嗨,杰罗德!谢谢!!!问题已解决。我没有正确理解你的答案是我的错。因此,无论代码是否会引发异常,我都需要捕获 FNFE。我现在明白“未处理”是什么意思了。将new FileReader 放入trycatch(FileNotFoundException e) ... 后,代码不再显示任何错误,我可以正常读取文件。原谅我的愚蠢,再次感谢!衷心感谢您的耐心等待。
【解决方案2】:

您正在调用的FileReader 构造函数指定Creates a new FileReader, given the name of the file to read from javadocs。这有点令人困惑,但看起来它接受文件名,而不是文件路径。您应该尝试使用其他构造函数,例如:

Reader reader = new FileReader(new File(path));

【讨论】:

  • 嗨彼得!感谢您的建议。所以我尝试了你的方法,但它仍然抛出 FileNotFoundException。我检查了“文件文件=新文件(路径);System.out.println(file.exists());”以及它返回true。文档说“FileNotFoundException - 如果文件不存在,是目录而不是常规文件,或者由于某些其他原因无法打开读取。”不知道你有没有其他想法?
【解决方案3】:

在给定的图像中,weatherstack.json 文件显示在资源目录中,而在目标文件中,它显示在类目录中。删除您的目标目录并重建您的项目。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多