Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can't read a file

 
C Law
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I tested my understanding of file I/O with
the following code and it's problematic.

Basically, I want to read lines of characters from
the file test.dat and store them into a String
ArrayList.

The error I got is java.io.FileNotFoundException: test.dat (The system cannot find the file specified)

While in reality the file was just there in the same directory
as the source code file.

Appreciate any advice.


 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileNotFoundException would be better named CantAccessFileException. It addition to the file actually not being found (which you say is not the case), it might mean a permission problem (the Java program has no access rights), or maybe the file is still open by another process.
 
C Law
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Ulf.

If it's a file permission problem, do you know any workaround?
 
Paul Clapham
Sheriff
Pie
Posts: 20966
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is a permissions problem, the "workaround" is to ask the owner of the file to change its permissions so you can read it. But that's probably you, and it probably isn't a permissions problem at all. It is maybe the problem where you don't understand the concept "current working directory".

Your code tries to look for the file in the current working directory, since you provide only the file's name and no path information. You seem to be assuming that your current working directory is the one where your Java source code is stored. This is not necessarily the case. It is whatever you set it to be before running the class. Or whatever your IDE set it to, if you are not running the class from the command line.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic