• Post Reply Bookmark Topic Watch Topic
  • New Topic

bufferedreader / accessing file

 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
i'm having trouble getting the syntax right for having a buffered reader access a specific file. i've tried putting this code under my main method:



when i do this, it says "filenotfoundexception". when i try putting the full filepath (/Users/.../HangmanLexicon.txt) it either says the same, or says syntax cannot be resolved.

i've also tried initializing it outside of the main method as:



this gives the same message.

i'm probably missing something very simple, so any help would be much appreciated!
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error means exactly what it says; the application is unable to find the file on the location you are specifying.

Where exactly is this file located?
Where are you running your application from?
 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The file is located in the same folder as the program... so I should be able to locate it by just specifying the filename, right?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mary caldwell:
The file is located in the same folder as the program... so I should be able to locate it by just specifying the filename, right?


In theory yes.
However, you need to double check if the folder containing your class files also contains this file.
 
mary caldwell
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, it is located in the same folder as both the .java and .class files for the program.... is there any syntax error i could be overlooking?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange.
Can you please try out

Notice the "/" before the file name.

You can also check out if there are no typos in the file name and the case is correct. Long time back I had a similar problem because the file name was xxx.JPG and I was defining it as xxx.jpg. Strange but true

Can't think of anything beyond this.
Anybody else got a clue?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by mary caldwell:
The file is located in the same folder as the program... so I should be able to locate it by just specifying the filename, right?


No. The class files are read from the classpath. Your filename will be considered relative to the "current directory".

You can find out where Java is searching your file by adding

System.out.println(new File("HangmanLexicon.txt").getAbsolutePath());

to your code.

If you want to load the file from the classpath, use getClass().getResourceAsStream() instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!