Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
        }
    }
like image 564
Sugarcoder Avatar asked Oct 22 '15 00:10

Sugarcoder


People also ask

What is a filenotfoundexception in Java?

Our file does not exists, so it must return a false. To Conclude, the FileNotFound Exception comes from IO system namespace of the object class. FileNotFoundException is responsible for occurring at times when we pass a file or are attempting to execute input or output operations with file but the file does not exists.

What happens if a file does not exist in Java?

If the file does not exist, the application creates it. However, if the file cannot be created, is a directory, or the file already exists but its permissions are sufficient for changing its content, a FileNotFoundException is thrown. FileNotFoundExceptionExample_v2.java

How to handle the exception when there is no such file?

There are other remedies to handle the exception: If the message of the exception tells that there is no such file or directory, then you re-verify whether you mentioned the wrong file name in the program or file exists in that directory or not.

Why does my IntelliJ program list as file input?

@Jack I checked the directory, and it lists as "File input" after I ran the program. 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.


1 Answers

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.

like image 63
Devansh Maurya Avatar answered Oct 02 '22 16:10

Devansh Maurya