Java: append string to txt file
foreword
This article mainly shares a way to append and write strings to txt files in Java.
1. Code example
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("C:\\Users\\user\\Desktop\\结果输出.txt", true));
for (String out : outPutString) {
try {
bw.newLine();
bw.write(out);
bw.flush(); //很重要,要不然可能输出的字符串不完整!
} catch (IOException e) {
e.printStackTrace();
}
}
}
}catch (Exception e){
e.printStackTrace();
}
finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}