我目前擁有的是這個import java.io.*;import java.security.acl.NotOwnerException;import java.util.Scanner;class Main { public static void main(String args[]) { try{ System.out.print("Enter username: "); Scanner scanner = new Scanner(System.in); String username = scanner.nextLine(); System.out.print("Enter email: "); Scanner scanner2 = new Scanner(System.in); String email = scanner.nextLine(); System.out.print("Enter password: "); Scanner scanner3 = new Scanner(System.in); String password = scanner.nextLine(); // Create file FileWriter fstream = new FileWriter("username.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write(username + ":" + email + ":" + password); //Close the output stream out.close(); }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } }}但它一直在清除第一行。有沒有辦法讓它每次都添加一行而不是清除第一行?如果可以,請有人幫助我。我正在為 java 使用 repl.it。
1 回答

慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
您需要打開帶有附加標志的文件為真。
FileWriter fstream = new FileWriter("username.txt",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write("\n" + username + ":" + email + ":" + password);
這將確保附加文件中的內容。此外,“\n”將確保每一行都打印在文件的新行上。
添加回答
舉報
0/150
提交
取消