Search code examples
javafilefilewriter

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


So I have seen alot of similar questions to this one but can't seem to find the answer so sorry if this is a duplicate. So I am creating a java program for black jack that I would like to have a system in place to save the chips the user has. I can get it to work but whenever I try to get it to save it just seems to overwrite what was already previously there. Example User enters there name: Bob The system automatically knows there chips so when they press the button to save game it writes there name and chips like so...

Bob

200 (or however many chips they have)

The problem comes up when a new user enters there name and saves so say sally was saving instead of going

Bob

200

Sally

300

It does

Sally

300 And completly deletes 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());
        }
    }

Here is my code that I have that is where my issue lies. Here is the whole code of the program method.

 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

Solution

  • Use following code.

    FileWriter fileWriter = new FileWriter(file, true);
    

    Here true signifies that you want to append data to existing file.

    For more details : https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html#FileWriter(java.io.File,%20boolean)