1

I was trying to write a piece of Java code to read each line from a file and add it to an Arraylist. Here is my code:

// try catch block removed for clarity.
file = new TextFileIn("names.txt");
String line = null;
while ((line = file.readLine()) != null) {
    list.add(line);
}

names.txt was located in the same package folder as my code. The IDE I used was Eclipse. When I run the code however I got a FileNotFoundException. The TextFileIn class comes from http://www.javaranch.com/doc/com/javaranch/common/TextFileIn.html

How could I get the file to be found?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
learnjourney
  • 653
  • 1
  • 6
  • 14
  • Have you tried using the full path of "names.txt"? If this is an acceptable workaround, it should do the trick. – Demento Apr 10 '11 at 14:13

3 Answers3

2

You're relying on the current working directory when accessing the file. The working directory depends on how you started the application. In Eclipse it's usually the project root folder. In commandline it's the currently opened directory. You cannot control this from inside the Java code, so this is considered a poor practice for files which are supposed to be part of the application itself.

The file is apparently in the same package as the running code, thus it's already in the classpath and then you could (and should) just get it straight from the classpath.

InputStream input = getClass().getResourceAsStream("names.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
String line = null;

while ((line = reader.readLine()) != null) {
    // ...
}

If you're calling this in static context, then replace getClass() by YourClassName.class where YourClassName is the name of the class where this code is sitting in.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
1

It is located in the package folder.
"names.txt" is referencing to the project root. So you have to search in your source folder like "src/your.package/names.txt".

Thomas Jungblut
  • 20,854
  • 6
  • 68
  • 91
0

You should lern how to compile and run programs from the command line, and later, how to change the settings of your IDE, to do the same. The basic knowledge is something you can carry with you from IDE to IDE, from eclipse to netbeans, to IntelliJ... .

Even if you're running from the desktop by clicking or from an IDE, you're sitting virtually in a folder of your OS. In eclipse, it is by default your classes-dir when running the code, but can be modified in one of the run as tabs, and it is ${PROJECT}/bin or ${PROJECT}/classes, wherever your classes go. So you have to place your file there, but attention - it might get deleted if you press 'clean all' or something, so use a copy.

Instead of funny TextFileIn, why don't you use java.util.Scanner?

Scanner scanner = new Scanner ("names.txt");
while (scanner.hasNextLine ())
    String line = scanner.nextLine ();
user unknown
  • 35,537
  • 11
  • 75
  • 121
  • I was following the exercises on java ranch cattle drive, and it's the question requirement is that I use TextFileIn, in case you are curious :). – learnjourney Apr 15 '11 at 13:03
  • I know JavaRanch, but a link to that tutorial would be fine. Scanner isn't the youngest kid in town - in former times (1.4) it was a bit of a hassle to work with files: Wrapper around wrapper around wrapper - `r = new BufferedFileReader (new InputStreamReader (new LineInputStream (new File (name)))))));` or something like that. So a lot of teachers made a more easy to use class, and now their material spreads and spreads, and isn'T updated to the new Scanner.class, which is part of the JDK/JRE. Maybe the ranch should update their page? – user unknown Apr 15 '11 at 15:43