Sugarcoder
Sugarcoder

Reputation: 177

IntelliJ "FileNotFoundException", File Exists

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

Upvotes: 14

Views: 30013

Answers (3)

Devansh Maurya
Devansh Maurya

Reputation: 1012

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.

Upvotes: 8

Zero
Zero

Reputation: 1152

Oh, I also have some problems about that. So try Edit Configurations. You can find it at Run -> Edit Configurations.. Then edit Working directory to where your file locates. By the way, you can edit it to base directory and add path in your code.

Upvotes: 3

antonpp
antonpp

Reputation: 2393

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. 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.

Upvotes: 3

Related Questions