File manipulation is really confusing!
I have a tic tac toe game, and I have a file that stores information about all the different people who played my tic tac toe game.
Sometimes an existing player comes in and wants to play again. So, if he wins or loses, I have to update the number of wins and losses he has, in the file.
Except the entire file keeps on getting deleted!!!! Here's exactly what happens: let's say I have ten players in my file. Two of these players get called and successfully retrieved. Then, after the game, I want to overwrite both of the new players' information with their new information. What winds up happening is all ten players are overwritten by the new player info. So, I started off with ten players, and end up with one.
I only want to overwrite two player's information, and keep everything else intact.
How do I do that??
Here's my function that updates the records of a given player:
public void updateRecords(Player player, java.io.File file) { String playerName = player.name; String filePlayer = " "; Scanner inputFile = null; // create a scanner object that can read info from the file try { inputFile = new Scanner(file); } catch (FileNotFoundException ex) { System.err.println("FileNotFoundExcpetion: " + ex.getMessage()); } // locate the player who needs to be updated while(inputFile.hasNext() && !playerName.equals(filePlayer)) { filePlayer = inputFile.next(); } // update the record with the new player info try { java.io.PrintWriter output = new java.io.PrintWriter(file); System.out.println(player.name + " wins = " + player.numWins); output.println(player.name + " " + player.symbol + " " + player.numWins + " " + player.numLosses); output.close(); } catch (FileNotFoundException ex) { System.err.println("FileNotFoundExcpetion: " + ex.getMessage()); } }