使用场景
特殊处理文件中有问题的部分,在读的过程中过滤掉错误的内容,然后再重新写入到文件中。
代码
private void preDeal(File file) throws IOException {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "GBK"))) {
LOG.info("开始读取{}文件", fileName);
String str;
while ((str = bufferedReader.readLine()) != null) {
fileRowList.add(str);
}
LOG.info("读取{}文件完成", fileName);
}
LOG.info("开始向{}文件填充XX数据", fileName);
fileRowList.addAll(fileRowList.size() - 1, new ArrayList<>());
LOG.info("向{}文件填充XX数据完成", fileName);
try (BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "GBK"))) {
fileRowList.forEach(v -> {
try {
bufferedWriter.write(v);
bufferedWriter.newLine();
} catch (IOException e) {
LOG.error("重{}}文件出错", fileName);
throw TsieExceptionUtils.newBusinessException(e);
}
});
bufferedWriter.flush();
}
}
待优化的点