IntelliJ "FileNotFoundException", File Exists
Asked Answered
F

3

14

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();
        }
    }
Foreplay answered 22/10, 2015 at 0:59 Comment(7)
most likely inputData doesn't point to the file you think it points to. Try with absolute path or simply try to print the absolute path of inputData and look what the output is.Gallagher
Print System.getProperty("user.dir"); and see if the directory is the one where the file is. If not, use getAbsolutePath()Benzedrine
I got the absolute path and the file is there for sure, but still error message. :(Foreplay
Are you sure the file extension is not messed up? Few times in Windows file extensions are hidden for a known types. Try listing all files from respective directory using your program and see if it lists input.txtAltheaalthee
@Jack I checked the directory, and it lists as "File input" after I ran the program.Foreplay
Add the output of the program to the question. Also add the output of the program when you listed the files in the directoryAltheaalthee
"I solved the issue by moving my "input.txt" file out of src folder and into the main project's folder." Yes that worked for me too :-)Charlton
I
8

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.

Indianapolis answered 23/10, 2018 at 5:24 Comment(0)
S
3

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.

South answered 22/10, 2015 at 1:12 Comment(1)
I got the absolute path, and the same error message popped up.Foreplay
D
3

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.

Dapsang answered 24/2, 2018 at 2:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.