Read text file line by line in Java
Description
The following code shows how to read text file line by line.
Example
//from w w w . ja va2s. co m
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static List<String> readLines(File file) throws Exception {
if (!file.exists()) {
return new ArrayList<String>();
}
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> results = new ArrayList<String>();
String line = reader.readLine();
while (line != null) {
results.add(line);
line = reader.readLine();
}
return results;
}
}