Java 文件的读取,写入,复制,压缩,解压等...相关操作,持续更新
1. 文本文件的读取
代码中使用到了BufferedReader-缓冲字符输入流 可以大大提高效率
/**
* 逐行读取文本
*
* @param filePath 文件路径
* @return List<String>
*/
public static List<String> readTxtFile1(String filePath) throws IOException {
Path path = Paths.get(filePath);
//判断文件是否存在
if (!Files.exists(path)) {
log.error("file is not exist");
return null;
}
List<String> txtList = new ArrayList<>();
try (InputStreamReader read = new InputStreamReader(Files.newInputStream(path), StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(read)) {
String lineTxt;
while (null != (lineTxt = bufferedReader.readLine())) {
if (StringUtils.isNotEmpty(lineTxt)) {
txtList.add(lineTxt);
}
}
}
return txtList;
}