java按行读取文件

本文记录java按行读取文件的方式。

FileReader+BufferedReader

1
2
3
4
5
6
7
8
9
10
11
12
private  List<String> getFromFile(String path) throws Exception {
FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
List<String> data = new ArrayList<>(10);
String line;
while ((line = br.readLine()) != null) {
data.add(line);
}
br.close();
reader.close();
return data;
}