IntelliJ "FileNotFoundException", File Exists

Sugarcoder picture Sugarcoder · Oct 22, 2015 · Viewed 15k times · Source

My objective is to read a text file on IntelliJ. However, when I ran my codes, I get a "FileNotFoundException" message. 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.

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]

I solved the issue by moving my "input.txt" file out or src folder and into the main project's folder.

I also used Scanner instead of 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();
        }
    }

Answer

Devansh Maurya picture Devansh Maurya · Oct 23, 2018

The issue is because of the differences in the working directory, i.e., the directory where your source code resides and the current working directory of IntelliJ is different. 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. After selecting Edit Configurations..., check out the option Working directory and change it accordingly.