So my code basically asks the user to enter an integer and that integer is saved into a file by writing it. However, when I want to add another integer, it just replaces the previous integer entered. How do I add the new integer in file without replacing the previous one?
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.