Monday, September 3, 2012

Reading text file - Java 7

try(FileReader fileReader = new FileReader("src/myfile.txt");
    BufferedReader reader = new BufferedReader(fileReader)){
           
    String line = null;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
}

Assumptions:
1. myfile.txt is available in eclipse's src directory.