1private ArrayList<String> readFileLines(String filepath) throws FileNotFoundException, IOException{
2 File fp = new File(filepath);
3 FileReader fr = new FileReader(fp);
4 BufferedReader br = new BufferedReader(fr);
5
6 ArrayList<String> lines = new ArrayList<>();
7 String line;
8 while((line = br.readLine()) != null) { lines.add(line); }
9
10 fr.close();
11 return lines;
12}
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 }