IntelliJ "FileNotFoundException", File Exists

14
我的目标是在IntelliJ上读取一个文本文件。然而,当我运行我的代码时,会弹出一个"FileNotFoundException"的错误信息。我的文件存在,我已经三次检查确保路径正确。我在Stack Overflow上仔细搜索了答案,读了每一个相关的问题,但是没有人提供一个具体的解决方案来解决这个问题。
这是我的代码:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;


    public class LetterGrader {

        public static void main(String[] args) {

           String curDir = new File(".").getAbsolutePath();
           System.out.println("Current sys dir: " + curDir);


           try {
               File inputData = new File("input.txt");
               BufferedReader br = new BufferedReader(new FileReader(inputData));

           } catch (IOException e) {
               System.out.println("File not found");
               e.printStackTrace();
           }
       }
    }

这是出现的错误信息。

    File not found
    java.io.FileNotFoundException: input.txt (The system cannot find the file specified)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(FileInputStream.java:195)
        at java.io.FileInputStream.<init>(FileInputStream.java:138)
        at java.io.FileReader.<init>(FileReader.java:72)
        at LetterGrader.main(LetterGrader.java:23)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

    Process finished with exit code 0
任何帮助都将不胜感激!当我找到解决方案时,我也会回复。[更新] 我通过将“input.txt”文件从src文件夹移动到主项目文件夹中来解决了这个问题。我还使用了Scanner而不是BufferedReader。我最终的可行代码:
        try {
            Scanner diskScanner = new Scanner(new File("input.txt"));
            while (diskScanner.hasNextLine()) {
                System.out.println(diskScanner.nextLine());
            }
            diskScanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

很可能 inputData 指向的不是你认为的文件。尝试使用绝对路径或者直接打印 inputData 的绝对路径并查看输出内容。 - user4668606
打印 System.getProperty("user.dir"); 并查看目录是否为文件所在的目录。如果不是,则使用 getAbsolutePath() - sam
你确定文件扩展名没有出错吗?在Windows中,某些已知类型的文件扩展名可能会被隐藏。请尝试使用您的程序列出所在目录中的所有文件,并查看它是否列出input.txt。 - suman j
@Jack,我检查了目录,在运行程序后它列为“文件输入”。 - Sugarcoder
将程序的输出添加到问题中。同时,当您列出目录中的文件时,请添加程序的输出。 - suman j
显示剩余2条评论
3个回答

7
问题是由于工作目录不同引起的,即源代码所在的目录和IntelliJ的当前工作目录不同。通常,工作目录是您项目的主目录。 因此,要么将文件放在该目录中,要么使用“编辑配置...”选项。选择“编辑配置...”后,查看“工作目录”选项并相应地更改它。

3

您可以先尝试打印文件的绝对路径

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

接下来,由于您正在使用IntelliJ,您可以在IDE中打开终端,并尝试从那里打开此文件。例如:

cat /Users/antonpp/Documents/Projects/testapp/input.txt

所以,您可以完全确定文件存在并且位于正确的位置。

1
我获取了绝对路径,但是出现了相同的错误信息。 - Sugarcoder

3

哦,我也遇到了一些关于这个的问题。尝试使用Edit Configurations。你可以在运行菜单中找到它->编辑配置.. 然后编辑工作目录到你的文件所在的位置。顺便说一下,你可以将其编辑为基本目录,并在代码中添加路径。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接