This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

READING FROM A TEXT FILE

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am a little confused here and was wondering if anyone can help.

For a bigger project, I am having to read from a text file looking like (file name is credentials.txt):




My code to read from the file:



When I test this code in JGrasp, it works fine, but when I am testing this on NetBeans 8.2 it gives me an error like:

java.io.FileNotFoundException: credentials.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at authenticationnb.AuthenticationNB.main(AuthenticationNB.java:32)

I have been at it for hours and cannot figure out the problem. Can anyone help please.

Cheers,
Abu
 
Saloon Keeper
Posts: 5707
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NetBeans probably has a different idea what the default directory is. Try using an absolute file path, meaning one that includes all directories, starting from the root.
 
Abu Alam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim, but I am still confused.

Can you please check if this will work?



I am sorry, it has only been 7 weeks since I started Java.

Thank you for all the help.
Abu
 
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To find out where it thinks it's looking you can try:
 
Abu Alam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tim,
Java is looking for the data file in the directory that it is in.

C:\Users\User\Desktop\AuthenticationNB\credentials.txt

So I made the following changes,

           

and it still gives me the FileNotFoundException

C:\Users\User\Desktop\AuthenticationNB\credentials.txt
java.io.FileNotFoundException: C:\Users\User\Desktop\AuthenticationNB\credentials.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at java.io.FileReader.<init>(FileReader.java:58)
at authenticationnb.AuthenticationNB.main(AuthenticationNB.java:34)
 
Bartender
Posts: 20924
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm not mistaken, you can simplify:


Regardless, if the absolute path printed is correct and you get a FileNotFoundException, do these 2 things:

1. Have someone else look at the code. After too many decades I still see what's supposed to be there and not what really is. Even the most junior of programmers can be a big assist here!!

2. Check the filesystem access rights for the file and the directory that the file is in for the userID that the application is running under.
 
Abu Alam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that Tim,

Cheers,
 
this is supposed to be a surprise, but it smells like a tiny ad:
Java Code Review and Psychology
https://coderanch.com/t/714798/java/Java-Code-Review-Psychology
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!