多线程 N 次写文件
public class FileUtils {
public static synchronized void writeFile(String filePath, String content) {
FileOutputStream outStream = null;
BufferedWriter bfWriter = null;
try {
outStream = new FileOutputStream(filePath, true);
bfWriter = new BufferedWriter(new OutputStreamWriter(outStream, "UTF-8"));
bfWriter.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bfWriter != null) {
bfWriter.close();
}
if (outStream != null) {
outStream.close();
}
} catch (Exception e) {
}
}
}
}问题描述
改进版本一
JDK 中 Writer 源码分析
改进版本二
测试
总结
Last updated