It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Can't read a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can Watch "Can New topic
Author

Can't read a file

C Law
Greenhorn

Joined: Mar 05, 2006
Posts: 21
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
Marshal

Joined: Mar 22, 2005
Posts: 41182
    
  45
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.


Ping & DNS - my free Android networking tools app
C Law
Greenhorn

Joined: Mar 05, 2006
Posts: 21
Thanks, Ulf.

If it's a file permission problem, do you know any workaround?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Can't read a file
 
Similar Threads
Comparing two huge files
Java APIs for Text file comparison
reading a text file from a JSP page
Java interprocess communication