简体   繁体   English

IntelliJ“FileNotFoundException”,文件存在

[英]IntelliJ “FileNotFoundException”, File Exists

My objective is to read a text file on IntelliJ. 我的目标是阅读IntelliJ上的文本文件。 However, when I ran my codes, I get a "FileNotFoundException" message. 但是,当我运行我的代码时,我收到“FileNotFoundException”消息。 My file exists. 我的档案存在。 I triple-checked to make sure that the path is correct. 我进行了三重检查以确保路径正确。 I've scoured Stack Overflow looking for an answer, read every question I've come across, but no one offered a concrete solution to the issue. 我已经搜索了Stack Overflow寻找答案,阅读我遇到的每个问题,但没有人提出具体的解决方案。

This is my code: 这是我的代码:

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

This is the error message that showed up. 这是出现的错误消息。

    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

Any help would be greatly appreciated! 任何帮助将不胜感激! When I find a solution, I will respond back, too. 当我找到解决方案时,我也会回复。

[UPDATE] [UPDATE]

I solved the issue by moving my "input.txt" file out or src folder and into the main project's folder. 我通过将“input.txt”文件或src文件夹移动到主项目的文件夹中解决了这个问题。

I also used Scanner instead of BufferedReader. 我还使用Scanner而不是BufferedReader。

My final code that worked: 我的最终代码有效:

        try {
            Scanner diskScanner = new Scanner(new File("input.txt"));
            while (diskScanner.hasNextLine()) {
                System.out.println(diskScanner.nextLine());
            }
            diskScanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

You can try to print absolute path of the file first 您可以尝试首先打印文件的绝对路径

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

Then, since you are using IntelliJ, you can open terminal right in IDE and try to open this file from there. 然后,由于您使用的是IntelliJ,您可以在IDE中打开终端并尝试从那里打开该文件。 For example: 例如:

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

So, you will be totally sure that file exists and it is in the right place. 因此,您将完全确定该文件存在且位于正确的位置。

The issue is because of the differences in the working directory, ie, the directory where your source code resides and the current working directory of IntelliJ is different. 问题是由于工作目录的不同,即源代码所在的目录和IntelliJ的当前工作目录不同。 Generally, the working directory is the main directory of your project. 通常,工作目录是项目的主目录。 So, either you place your file in that directory, or use Edit Configurations... option. 因此,要么将文件放在该目录中,要么使用“ Edit Configurations...选项。 After selecting Edit Configurations... , check out the option Working directory and change it accordingly. 选择“ Edit Configurations... ,请选中“ Working directory ”选项并相应地进行更改。

Oh, I also have some problems about that. 哦,我也有一些问题。 So try Edit Configurations . 因此,请尝试Edit Configurations You can find it at Run -> Edit Configurations.. Then edit Working directory to where your file locates. 您可以在运行 - >编辑配置中找到它。然后编辑Working directory到您的文件所在的位置。 By the way, you can edit it to base directory and add path in your code. 顺便说一句,您可以将其编辑到基目录并在代码中添加路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM