File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Can't read a file

C Law

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

The error I got is 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

Joined: Mar 22, 2005
Posts: 42965
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

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

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

Joined: Oct 14, 2005
Posts: 19973

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.
I agree. Here's the link:
subject: Can't read a file
jQuery in Action, 3rd edition