亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將字符串存儲在 .txt 文件中,同時不清除該文件中已有的內容 java

如何將字符串存儲在 .txt 文件中,同時不清除該文件中已有的內容 java

忽然笑 2021-09-12 16:08:43
我目前擁有的是這個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”將確保每一行都打印在文件的新行上。


查看完整回答
反對 回復 2021-09-12
  • 1 回答
  • 0 關注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號