Question

I know that some people have already encountered this problem, but I am still unable to solve it.

Here is my code:

import java.io.*;

public class MainClass
{
    public static void main( String[] args )
    {
       BufferedReader br = new BufferedReader(new FileReader("dump.txt"));
       String line;
       int i = 0;
       int j = 0;
       while((line = br.readLine()) != null){

// And so on...

Basically, I copied my text file in the project directory, in a subdirectory (specifying the path), in the workspace main directory, but Eclipse continues to throw a FileNotFoundException. Besides, I have already tried with refreshing the project files in the left column, but without results.

Was it helpful?

Solution

public static void main(String[] args) {

        BufferedReader br = null;
        try {
            String sCurrentLine;
//          br = new BufferedReader(new FileReader("D:\\test.txt")); //Absolute File
            br = new BufferedReader(new FileReader("test.txt"));//Relative File
            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }

Relative file location

 MyProject
 --src
    --Main
 --test.txt

OTHER TIPS

Try with your absolute path of the file. If you are using IDE place the file in your src folder or place in separate folder and give the file path as foldername/filename.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top