[英]How to add a new line when writing to a text file?
所以我的代码基本上要求用户输入一个整数,然后通过写入将该整数保存到文件中。 但是,当我想添加另一个整数时,它只会替换之前输入的整数。 如何在文件中添加新整数而不替换前一个?
public static void main(String[] args) throws IOException {
File file = new File("Text.txt");
FileWriter fw = new FileWriter(file);
PrintWriter writer = new PrintWriter(fw);
int steps = Integer.parseInt(JOptionPane.showInputDialog("How many steps"));
writer.println(steps);
writer.close();
}
您必须通过发送true
参数以“追加”模式初始化FileWriter
对象。
FileWriter fw = new FileWriter(file , true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.