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.

有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top