如何让我的 FileWriter 找到下一个可用行而不是在下面写

How to get my FileWriter to find the next available line than write below that

所以我看到了很多与此问题类似的问题,但似乎找不到答案,如果这是重复的,我深表歉意。所以我正在为 black jack 创建一个 java 程序,我希望有一个系统来保存用户拥有的筹码。我可以让它工作,但每当我试图让它保存时,它似乎只是覆盖了之前已经存在的内容。 例子 用户在那里输入姓名:Bob 系统会自动识别那里的筹码,所以当他们按下按钮保存游戏时,它会像这样写名称和筹码...

鲍勃

200(或者他们有多少筹码)

当新用户输入名称并保存时出现问题,所以说 sally 正在保存而不是去

鲍勃

200

莎莉

300

确实如此

莎莉

300 并完全删除 bob

    public void newSave(String user){
        this.user = user;
        user = user;
        String Chip = Integer.toString(chips);
        try{
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(user);
            fileWriter.write("\n");
            fileWriter.write(Chip);


            fileWriter.close();
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

这是我的代码,这就是我的问题所在。 这是程序方法的全部代码。

 import java.util.*;
 import java.io.*;

 class BlackJackPlayer{
 //Keep the data secure by using private
 private String hand;
 private String user;
 private int betNum;
 private int sum;
 private int numAces;
 private int chips = 100;
 private String bet;
 private static Random gen = new Random();
 private String result = "";
 public String Win = "Win", Lose = "Lose", Split = "Split";
 private final int ACE = 1;
 private final int JACK = 11;
 private final int QUEEN = 12;
 private final int KING = 13;
 //Scanner for fileIn
 Scanner fileIn;
 //File For Saves!
 File file = new File("Saves.txt");
 //Array For Saves!
 private ArrayList<String> User = new ArrayList<>();
 private ArrayList<Integer> Chips = new ArrayList<>(); 



 //constructor
 public BlackJackPlayer(){
  hand = "";
  sum = 0;
  numAces = 0;

 //Create a file if save file does not exist//
     try{
  if (file.createNewFile()){
     System.out.println("Save File Created!");
  }
  else{
     //Say Nothing//
  }
  }//End Try
  catch(Exception e){
     System.out.println(e.getMessage());
  }
  //End File Create//

  //Read File //  
  try{
     fileIn = new Scanner(new FileReader(file, true));
     while(fileIn.hasNext()){
        User.add( fileIn.nextLine().trim() );
        Chips.add( fileIn.nextInt() );
     }//End While
     fileIn.close();
  }//End Try
  catch(Exception e){
     System.out.println(e.getMessage());
   }//End Catch
   //End Read File//

   }//End public

   //checkSaveStatus//
   //See if save exists already in text than send to proper scenario//
   public boolean checkSaveStatus(String user){
  for(String u: User){
     if (user == u){
        return true;
     }//end if
    }//end for
    return false;

    }//end checkSaveStatus
   //End checkSaveStatus//




  //overWriteSave if save already exists//
  public void overWriteSave(String user){



  }//end overWriteSave
  //End overWriteSave//




  //newSave scnenario if save dosent exist//
  public void newSave(String user){
  this.user = user;
  user = user;
  String Chip = Integer.toString(chips);
  try{
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(user);
        fileWriter.write("\n");
        fileWriter.write(Chip);


        fileWriter.close();
  }
  catch(Exception e){
     System.out.println(e.getMessage());
  }

 }



 public boolean checkBet(String bet, int chips){
  this.bet = bet;
  int betNum = Integer.parseInt(bet);
     if(betNum <= chips){
     return true;
     }
  return false;
 }


  public String setBet(String bet){
  this.bet = bet;
  betNum = Integer.parseInt(bet);
     return "You Bet:  " + betNum; 
  }

  public String updateChips(){
  chips -= betNum;
  return "You have: " + chips + " chips";
  }



  //Getter for hand variable
  public String getHand(){
  return hand;
  }

   public String setHand(){
  hand = " ";
  return hand;
  }

 //Getter for sum variable
  public int getSum(){
  return sum;
  }

  public void hit(){
  //local variable
  int currentCard = gen.nextInt(13) + 1;

  if(currentCard > ACE && currentCard < JACK){
     sum += currentCard;
     hand += currentCard + "   ";
  }
  else if(currentCard == ACE){
     sum += 11;
     numAces++;
     hand += "A  ";
  }

  else if(currentCard == QUEEN){
     sum += 10;
     hand += "Q  ";
  }

  else if(currentCard == QUEEN){
     sum += 10;
     hand += "Q  ";
  }

  else if(currentCard == KING){
     sum += 10;
     hand += "K  ";
  }//Ends Else If

  //Is Ace 1 or 11
  if(sum > 21 && numAces > 0){
     numAces--;
     sum -= 10;
  }

  }//ENDS HIT

 public void stand(){
  sum = sum;
  return;
 }//ends stand


  public String getWin(BlackJackPlayer other) {
  if(sum > 21){
     result = Win;
  }
  else if(sum < other.getSum()){
     result = Lose;
  }
  else if(sum == other.getSum()){
     result = Split;

  }
  return result;
  }


  }//end class

使用以下代码。

FileWriter fileWriter = new FileWriter(file, true);

此处true表示您要将数据附加到现有文件。

更多详情:https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,%20boolean)