0

Im trying to read a simple text file with contents input.txt

Line 1
Line 2
Line 3

But it always goes to the exception and prints Error.

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String args[]){

        List<String> text = new ArrayList<String>();
        try{
            BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
            for (String line; (line = reader.readLine()) != null; ) {
                 text.add(line);
            }
            System.out.println(text.size()); //print how many lines read in
            reader.close();
        }catch(IOException e){
            System.out.println("ERROR");
        }
    }   
}

Im using Eclipse as my IDE if that makes a difference. I've tried this code on http://www.compileonline.com/compile_java_online.php and it runs fine, why wont it run in Eclipse?

Eduardo
  • 6,900
  • 17
  • 77
  • 121

4 Answers4

1

give complete file path like "C:\\folder_name\\input.txt" or place input.txt inside src directory of eclipse project.

Pravat Panda
  • 1,060
  • 2
  • 13
  • 27
1
public class Main {
    public static void main(String args[]){

        List<String> text = new ArrayList<String>();
        try{
            BufferedReader reader = new BufferedReader(
                    new FileReader("input.txt"));  //<< your problem is probably here, 
            //More than likely you have to supply a path the input file.
            //Something like "C:\\mydir\\input.txt"
            for (String line; (line = reader.readLine()) != null; ) {
                 text.add(line);
            }
            System.out.println(text.size()); //print how many lines read in
            reader.close();
        }catch(IOException e){
            System.out.println("ERROR"); //This tells you nothing.
            System.out.println(e.getMessage());  //Do this
            //or 
            e.printStackTrace(); //this or both

        }
    }   
}
Pete B.
  • 3,188
  • 6
  • 25
  • 38
  • is there a way I can refer to the current directory? – Eduardo Jun 21 '13 at 22:02
  • 2
    What you are doing does refer to the current directory, but I _suspect_ that is your problem. Until you have the stack trace you don't really know. – Pete B. Jun 21 '13 at 22:05
1

You most likely have a bad path. Consider this main instead:

public class Main {
    public static void main(String args[]) throws Exception {

        List<String> text = new ArrayList<String>();

        BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
        for (String line; (line = reader.readLine()) != null; ) {
             text.add(line);
        }
        System.out.println(text.size()); //print how many lines read in
        reader.close();
    }   
}

The "throws Exception" addition allows you to focus on the code, and consider better error handling later. Also consider using File f = new File("input.txt") and use that, because it allows you to print out f.getAbsolutePath() which tells you the filename it was actually looking for.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • 1
    The code you posted doesn't compile. `public class Main throws Exception {` is wrong, maybe you were talking about `public static void main(String args[]) throws Exception {`? – BackSlash Jun 21 '13 at 22:05
0

Changing input.txt to src\\input.txt solved the problem! I guess it was because the current directory isnt actually the src folder its the parent,

Thanks for the help!

Eduardo
  • 6,900
  • 17
  • 77
  • 121
  • 1
    When you just say `input.txt` then it will start looking in its root folder (where your src and other project related folder resides ), that means your project folder. When you say `src\\input.txt` that means `root folder\src\input.txt`. – Smit Jun 21 '13 at 22:24