Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileReader cannot find text file!  RSS feed

 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My program consistently says that it cannot find a text file I specify. Using both NetBeans and Eclipse, I had it show me the absolute path it's using, and put the txt file there. No effect. It's in the directory with bin and src directories.



//At this point, console shows:
C:\..directory tree...\FindMe.txt
Enter the name of your listing file:
listings.txt

Exception in thread "main" java.io.FileNotFoundException: listings.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at java.io.FileReader.<init>(FileReader.java:41)
at exercise.Main.main(Main.java:39)
Java Result: 1


//The file IS there; I have triplechecked the location and directory tree, etc. Why is it not being found??

Thank you to anyone who can let me know what is necessary!
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java may not be looking where you think it's looking for the file. If you aren't using a fully qualified file name, then Java will look for files relative to the user directory. You can find out where Java is looking by running this line in your program:



Also, "Joe Student" please respond to a private message I will send to you about an important administrative matter.
 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried that line of code; the directory it indicates is precisely where the listings.txt file is located. I have triplechecked this, and am looking at the indicated directory as I type this. The text file is there. Other suggestions???
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cross-posted: cannot-find-file-filereader

Please read: BeForthrightWhenCrossPostingToOtherSites
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I'll add them for you so that you can see the difference.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Spurgeon wrote:I have tried that line of code; the directory it indicates is precisely where the listings.txt file is located. I have triplechecked this, and am looking at the indicated directory as I type this. The text file is there. Other suggestions???

So clearly you're wrong. I would suggest instead of messing about assuming you know what current working directory is being used, you simply hard-code the full path to the file in the program you're writing to debug this problem.

It's also possible you're using Windows and you haven't fixed its stupid default where it hides the file extensions from you. If that were the case you might think you were looking at listings.txt whereas the actual name of the file might be listings.txt.txt; note also that Notepad and sometimes other programs conspire with this stupidity by adding another extension to the file name you provide.
 
David Spurgeon
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that was the case. The file's FULL name was listings.txt.txt Teerefore the program wouldn't find it. Stupid Windows. And stupid me.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!