/** * @author lautturi.com * Java example: try with resources catch Exception */ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.*; public class Lautturi { public static void main(String[] args) { String line; BufferedReader br; int no=0; try { br = new BufferedReader(new FileReader("test.txt")); try { while ((line = br.readLine()) != null) { no++; System.out.println("Line "+no +"=>"+ line); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
output:
Line 1=>lautturi Line 2=>hello world Line 3=>java Line 4=>python Line 5=>perl Line 6=>js