+6  A: 

You need "src/Hankees.txt"

Your file is in the source folder which is not counted as the working directory.\

Or you can move the file up to the root directory of your project and just use "Hankees.txt"

jjnguy
Yup, that worked! Can you explain why this happens? Afterall, the file is in the src folder where the .class files are stored, why do I have to define the src folder again?
PHLAK
Um, I believe eclips by design makes the toplevel directory of your project the working directory of the program. I don't know why they do it that way. And lots of people have /bin and /src folders, so the /src isn't where the .class files are.
jjnguy
what if you have the file in a folder under the root directory, like Baseball/players/Hankees.txt?
norabora
That is the path you should use then. `Baseball/players/Hankees.txt`
jjnguy
Assuming you have: `ProjectName` : `Baseball` : `players` : `Hankees.txt`
jjnguy
What if it's just ProjectName : players : Hankees.txt?
norabora
Then use the path - `players/Hankees.txt`
jjnguy
Got it! Thanks for your help!
norabora
No problem. (15 chars)
jjnguy
+2  A: 

Yeah, eclipse sees the top directory as the working/root directory, for the purposes of paths.

...just thought I'd add some extra info. I'm new here! I'd like to help.

mieze
A: 

Paraphrasing from http://java.sun.com/javase/6/docs/api/java/io/File.html:

The classes under java.io resolve relative pathnames against the current user directory, which is typically the directory in which the virtual machine was started.

Eclipse sets the working directory to the top-level project folder.

Rob
+1  A: 

A project's build path defines which resources from your source folders are copied to your output folders. Usually this is set to Include all files.

New run configurations default to using the project directory for the working directory, though this can also be changed.

This code shows the difference between the working directory, and the location of where the class was loaded from:

public class TellMeMyWorkingDirectory {
    public static void main(String[] args) {
     System.out.println(new java.io.File("").getAbsolutePath());
     System.out.println(TellMeMyWorkingDirectory.class.getClassLoader().getResource("").getPath());
    }
}

The output is likely to be something like:

C:\your\project\directory
/C:/your/project/directory/bin/
Stephen Denne
+1  A: 

This is really similar to another question. http://stackoverflow.com/questions/6639/how-should-i-load-files-into-my-java-application

How should I load my files into my Java Application?

You do not want to load your files in by:

C:\your\project\file.txt

this is bad!

You should use getResourceAsStream.

InputStream inputStream = YourClass.class.getResourceAsStream(“file.txt”);

And also you should use File.pathSeparator(); which is the system-dependent path-separator character, represented as a string for convenience.

Bernie Perez
Good information to know, thanks!
PHLAK
A: 

Hi, i has the same problem. If YourClass and Hankees.txt have one directory in your Project then, i find following - not so bad.

import java.net.URL;
//<...>
URL url = YourClass.class.getClassLoader().getResource("Hankees.txt");
String filesPathAndName = url.getPath();     
Scanner myScanner = new Scanner(new File(filesPathAndName))
//<...>