我创建了这个方法来通过多个线程将文本写入同一个文件:
public static void ThreadsafeWriter(String text, String file) throws IOException {
String nodeValue = text;
BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
synchronized (writer) {
String[] words = nodeValue.split(" ");
for (String word: words) {
writer.write(word);
writer.newLine();
}
writer.close();
}
}
我想问一下有没有错误,会不会导致异常?
慕娘9325324
拉丁的传说
相关分类