您好,我正在尝试将字符串写入文本文件,但有一点问题。我在此站点的其他问题的帮助下完成了我的代码,但是当我尝试将字符串添加到文本文件时,它会删除该文本文件中的所有内容并写入输入。但是我想让它去下一行并写下来。我无法解决它。我将不胜感激任何帮助。谢谢..
public static void addCar() throws IOException{
String string = transferBrand;
String string2 = ":"+transferModel;
System.out.println(string+string2);
File file = new File("HatchBack.txt");
try {
StringReader stringReader = new StringReader(string+string2);
BufferedReader bufferedReader = new BufferedReader(stringReader);
FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for(String line = bufferedReader.readLine(); line != null; line =bufferedReader.readLine()) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
答案 0 :(得分:1)
(未经测试)您是否尝试过JavaDoc中提到的这个?
FileWriter fileWriter = new FileWriter(file, true);