Computer Homework :(
July 27, 2010 8:51 PM   Subscribe

Can you help me, helpful Java-knowers of the land??

Very frustrating problem, and my professor hasn't been of much help...

I'm using Eclipse for my java homework and I'm trying to use a FileReader to read a txt file, but I keep getting a FileNotFoundException when I use the relative pathname, even though I checked many times and it is almost definitely correct. It seems almost like a glitch.

Do you have any ideas for why this would be happening? Or any workarounds so I can do my homework? Much appreciated!
posted by malhouse to Computers & Internet (13 answers total)
Well, where are you looking for the file, i.e. relative to what? Seems like there's a good chance that the cwd of the program you're running isn't what you think it is. You could use something like this to figure out what your current dir for the program is:
posted by RustyBrooks at 8:53 PM on July 27, 2010 [1 favorite]

Response by poster: thanks for the quick reply!

here is my current dir : /Users/chun/Documents/workspace/jc3487_hw1

and so i use "1_courseList.txt" as the filename but it doesn't work even though it's in the project folder.
posted by malhouse at 9:01 PM on July 27, 2010

Can you paste the exception text?

Maybe you could just use the code you used to get your current directory and concatenate the file name to it, then try to read it -- something like:

File dir1 = new File (".");
String currentDir = dir1.getCanonicalPath();
String fullFileName = currentDir + "1_courseList.txt";

... then try to read it using fullFileName

Granted this isn't that elegant but your code should still be portable...

It's these sorts of ambiguous errors/error messages that always piss me off about java.
posted by thorny at 9:15 PM on July 27, 2010

Or you could just use the fully qualified path with file name if you just need it to run...
posted by thorny at 9:17 PM on July 27, 2010

Response by poster: Thanks thorny! With that code, here's the error I get: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException

at tester.main(

...but no "FileNotFoundException" -- do you have any thoughts? thanks so much.
posted by malhouse at 9:25 PM on July 27, 2010

wrap your code with



}catch(IOException e)
//error handling here if you need
posted by pyro979 at 9:33 PM on July 27, 2010

OK that code wasn't exactly in context -- more of a suggestion, but you might have to throw it inside a try/catch block. Maybe paste the code too? :)
posted by thorny at 9:33 PM on July 27, 2010

Was your first initial problem also a compilation error? If so, click on the red symbol to the left of the offending statement and Eclipse will give you a list of possible ways to fix the compilation problem.
posted by nightwood at 9:34 PM on July 27, 2010

also with the code thorny gave you:

File dir1 = new File (".");
String currentDir = dir1.getCanonicalPath();

debug currentDir to see where your code is looking, just to make sure it matches your expectations.
posted by pyro979 at 9:35 PM on July 27, 2010

Eclipse, by default sets the cwd to ${workspace_loc:[project folder]}, so in your case, the file should go in jc3487_hw1. The src folder should also be in there.

Check the "Run Configuration"s panel for the application and look under the "Arguments" tab. The working directory section is at the bottom.

Really, eclipse isn't the tool to be using for Java homework...
posted by klanawa at 9:59 PM on July 27, 2010

Response by poster: thanks so much, i will give these a try!
posted by malhouse at 10:17 PM on July 27, 2010

Response by poster: I'm gonna try using a try/catch block...if anyone else has ideas, please do share. thanks!
posted by malhouse at 10:23 PM on July 27, 2010

A file reader is basically the same as InputStreamReader(new FileInputStream(filename)) (line 66)

So we look at FileInputStream's constructor which takes a single String parameter. When String is not null, this is the same as FileInputStream(new File(fileName)) (see line 131)

So we have a File object - if we look at FileInputStream's constructor which takes a single File parameter. This attempts to open file.properPath(true) (see line 82)

So we look at the file object - the properPath method isn't public so we can't access it directly (line 1476) but we can access the very similar method getAbsolutePath(), which calls properPath(false) (line 461).

What's more, we can look at how properPath works (line 1491) and see it takes a relative path, finds
userdir = System.getProperty("user.dir");
then does some stuff to decide you need a forwardslash or a backslash as your file path separator, then returns userdir + path separator + relative path. So if we check the value of userdir, that will tell us the directory your program is looking in for your file.

Once we know this, we can write some test code to see what's going on:
public class Main {
    public static void main(String[] args) {
        File f = new File("1_courseList.txt");
        System.out.println("Absolute path to File f: " + f.getAbsolutePath());

        String userdir = System.getProperty("user.dir");
        System.out.println("Because user.dir is " + userdir);

        if (f.exists()) {
            System.out.println("File exists!");
        } else {
            System.out.println("File doesn't exist.");
    } // end of main method
If I run that on my system, I get the output:
Absolute path to File f: /home/michael/NetBeansProjects/pathTest/1_courseList.txt
Because user.dir is /home/michael/NetBeansProjects/pathTest
File doesn't exist.
Try the code on your system, and see what you get - most likely you'll find that Eclipse is running your code from a different directory to the one you expect. In my IDE, this is configurable in the project configuration.
posted by Mike1024 at 1:59 AM on July 28, 2010

« Older Recommend a new laptop?   |   How do I tighten up an XM Skydock in the cigarette... Newer »
This thread is closed to new comments.