So I'm practicing file I/O, but I know nothing about it in java.
I keep getting my file erased every time I call my function.
What happens is this:
I call the "createNewPlayer" function, and it creates the player and writes it to the player info to the file, no problem.
Then I call it again for the second player, and it immediately erases the player information, and then ends up putting in the new information, but it's missing the original info.
...here's the code.
public void play() throws Exception { boolean play; // ask user if he wants to play play = promptToPlay(); java.io.File file = new java.io.File("Tic-Tac-Toe.txt"); if(play) { boolean new_player; new_player = promptNew_Existing(); if(new_player) { [B]player1 = createNewPlayer(file);[/B] } else player1 = retrievePlayer(file); //get user name and stuff new_player = promptNew_Existing(); if(new_player) { [B]player2 = createNewPlayer(file);[/B] } else player2 = retrievePlayer(file); player1.inCellValue = 9; player2.inCellValue = 1; }
And then my createPlayer function....
Note the line that I originally commented out. That's when I was getting the erased file, so I've tried fixing the problem. I'm presuming that creating a new file erases the original one.
As a result, I'm trying to pass a file through the function, and then open the file.
However, I don't even know how to open a file in java lol. The syntax is apparently wrong. Plus I don't even know if this will prevent the file from being erased.
public Player createNewPlayer(java.io.File file) throws Exception { Player player = new Player(); // java.io.File file = new java.io.File("Tic-Tac-Toe.txt"); java.io.PrintWriter output; output.open(file); // receive player info player.receiveName(); player.receiveSymbol(); // write player information to file output.println(player.name + " " + player.symbol + " " + player.numWins + " " + player.numLosses); output.close(); return player; }
Thanks in advance.