0

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.

Pippo
  • 1,543
  • 3
  • 21
  • 40
  • 2
    First, try giving the absolute file path and see if your program works fine. If yes, then please post your project structure here, highlighting the file... We can check and try to help – Hirak Mar 11 '14 at 06:35
  • @Hirak Thanks for your reply. I have also tried with the full path, but again, no results. I will edit the question for further details on my class. – Pippo Mar 11 '14 at 06:39
  • When you run programs in eclipse, by default , the context is :// – Tejas Kale Mar 11 '14 at 06:42
  • check file is current directory – Benjamin Mar 11 '14 at 06:43
  • There should be really something related to Eclipse. Now I removed all the "guts" of the code, leaving only the input part, but still it throws an exception. And I have tried to copy the file everywhere (using the full path or just specifying the subdirectory in the project folder). – Pippo Mar 11 '14 at 06:52

2 Answers2

2
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
Hirak
  • 3,601
  • 1
  • 22
  • 33
0

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.

Shriram
  • 4,343
  • 8
  • 37
  • 64