我试图逐行读取字符串,并通过FileWriter将其写入文本文件。该文件必须覆盖,因此我不能在FileWriter中使用true作为第二个参数。在String到达writeToFile方法之前,我可以使用=来解析String,但是我只得到了最后一个String行,因为它被覆盖了。使用+ =,它获得所有字符串的一行字符串。我也尝试将String()拆分为多个,但它是相同的。我怎么能改变下面的代码,逐行获得输出?
由于
private void writeToFile(File cf, String ln) throws IOException {
//true won't work as the data must be overwrite.
FileWriter fw = new FileWriter(cf);
try (BufferedWriter bw = new BufferedWriter(fw)) {
// I got just one string all at the same line. Or just the last-
// String
bw.write(ln);
bw.newLine();
bw.flush();
}
}
private void writeToFile(File cf, String ln) throws IOException {
//true won't work as the data must be overwrite.
FileWriter fw = new FileWriter(cf);
try (BufferedWriter bw = new BufferedWriter(fw)) {
// I got just one string all at the same line. Or just the last-
// String
bw.write(ln);
bw.newLine();
bw.flush();
}
}