【工具】文件处理
程序员文章站
2024-01-14 10:22:16
...
/**
* @Description 按行读文件,每行加入List集合
* @Param String
* @return List<String>
**/
public static List<String> readFileByLines(String filename) {
List<String> result = null;
BufferedReader bufferedReader = null;
ClassPathResource resource = new ClassPathResource(filename);
if (resource.exists()) {
try {
result = new ArrayList<>();
InputStream fileInputStream = resource.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line;
while ((line = bufferedReader.readLine()) != null) {
result.add(line);
}
} catch (IOException e) {
log.warn("readFileByLines() - io error !");
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
log.warn("readFileByLines() - close reader error !");
}
}
}
} else {
log.warn("readFileByLines() - file is null, filename = " + filename);
}
return result;
}
上一篇: java中的数组工具类Arrays
下一篇: 【工具】定时器(servlet)