Surender Thakran
Surender Thakran

Reputation: 694

File IO in eclipse

I am very new to eclipse, i have following line in my code:

BufferedReader bf = new BufferedReader(new FileReader("dvdinfo.txt"));

when implemented through a simple text editor and windows console it works fine but when run through eclipse it shows FileNotFoundException.

Yes, i do always keep the txt file in the same folder as the .java file that contains this code. Also in eclipse i have selected to have both my source code and my class file in the same folder.

Is there any different way to specify the name of a file in the same folder in eclipse.

Note: i am using windows 7 and eclipse 3.7

Upvotes: 0

Views: 2499

Answers (2)

Qwerky
Qwerky

Reputation: 18455

Unexpected FNFEs (especially with unqualified file names) tend to be because you are not looking in the directory that you think you are. Is the qualified file name included in the stack trace?

Try this;

File file = new File("dvdinfo.txt");
try {
  BufferedReader bf = new BufferedReader(new FileReader(file));
  ......
} catch (FileNotFoundException fnfe) {
  //Note that writing to syserr is only for debugging - don't do this in prod
  System.err.println("Can't find " + file.getCanonicalPath());
  fnfe.printStackTrace();
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503489

Yes, i do always keep the txt file in the same folder as the .java file that contains this code.

That doesn't mean it's in the same directory when you're running the code.

Is there any different way to specify the name of a file in the same folder in eclipse.

Same folder as what? If you want the file to be packaged up with the output of the build, you can specify it as a resource in Eclipse, but then you should be reading it using Class.getResourceAsStream() or ClassLoader.getResourceAsStream().

If you really want it to be a file, you may want to either specify an absolute filename or change the working directory in the Run configuration in Eclipse.

Upvotes: 1

Related Questions