1 BufferedReader br = null;
2 try {
3 File file = new File("myfile.txt"); // java.io.File
4 FileReader fr = new FileReader(file); // java.io.FileReader
5 br = new BufferedReader(fr); // java.io.BufferedReader
6 String line;
7 while ((line = br.readLine()) != null) {
8 // process the line
9 }
10 }
11 catch(IOException e) { e.printStackTrace();}
12 finally
13 {
14 try { if (br != null) br.close(); }
15 catch(IOException e) { e.printStackTrace(); }
16 }
1 Scanner sc = null;
2 try {
3 File file = new File("myfile.txt"); // java.io.File
4 sc = new Scanner(file); // java.util.Scanner
5 String line;
6 while (sc.hasNextLine()) {
7 line = sc.nextLine();
8 // process the line
9 }
10 }
11 catch(FileNotFoundException e)
12 {
13 e.printStackTrace();
14 }
15 finally {
16 if (sc != null) sc.close();
17 }