• Post Reply Bookmark Topic Watch Topic
  • New Topic

try catch cannot read file  RSS feed

 
Irving Luna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im running this method but for some reason I am not able to find the data record, it keeps going to the catch. Can somebody let me know where I am messing up please. I have the data.txt file in the same directory as the java file.

 
Irving Luna
Greenhorn
Posts: 8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
solved the problem by placing the full path of the file
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done

Try a JFileChooser to find the files. You may have to pass null for the component for the dialogue to appear over.
 
Dileep donepudi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,Irving Luna.

There is no need to give the complete path , instead you can provide the relative path.
Means, suppose your java class and data.txt file are inside the com.company.project.module.games package under src folder then
you can declare the Scanner class as follows
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get started, using the full path to the file is a reasonable thing to do. Providing the relative path is also reasonable, but then you have to know what the current working directory is. (My impression is that a lot of beginners haven't even heard of the "current working directory" concept, which is unfortunate.)

However I don't think that assuming the code is going to be run from an IDE, as Dileep just did, is the right thing to do. Because as soon as you try to run the application outside the IDE, your CWD (current working directory...) is different and your application stops working.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!