Hi all is it possible to delete the last line of a text file each time a method is called. I have the following method:
Writer output = null;
File file = new File("playerData.xml");
try {
output = new BufferedWriter(new FileWriter(file, true));
output.write("\t" + "<player>" + "\n");
output.write("\t" + "\t" + "<playerName>" + playerName + "</playerName>" + "\n");
output.write("\t" + "\t" + "<playerScore>" + pointCount + "</playerScore>" + "\n");
output.write("\t" + "\t" + "<playerTime>" + minutes + " minutes " + seconds + " seconds" + "</playerTime>" + "\n");
output.write("\t" + "</player>" + "\n");
output.write("<indianaTuxPlayers>" + "\n");
output.close();
System.out.println("Player data saved!");
}
catch (IOException e) {
e.printStackTrace();
}
Basically what i want to do is every time the method is run delete the "<indianaTuxPlayers>" line. Then carry on writting the data from that position. So in otherwords i will end up with the following output after several method runs:
<?xml version="1.0" encoding="UTF-8"?>
<indianaTuxPlayers>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
</indianaTuxPlayers>
I've tried doing this using randomaccessfile, however as the file will be read as an xml a randomaccessfile method adds uncessary bytes! Any idea would be most welcome!