Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Copy one text file to another text file and delete last line

807588Mar 19 2009 — edited Mar 19 2009
Hi all wonder if someone can help, i want to be able to copy one text file to another text file and remove the last line of the first text file. So currently i have this 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("</indianaTuxPlayer>" + "\n");
		    output.close();
		    System.out.println("Player data saved!");
		}
	    catch (IOException e) {
			e.printStackTrace();
		}
However each time the method is run i get the "</indianaTuxPlayer>" line repeated, now when i come to read this in as a java file i get errors becuase its not well formed. So my idea is to copy the original file, remove the last line of that file, so the </indianaTuxPlayer> line and then add this to a new file with the next data saved in it. So i would end up with something like this:

<?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>

However after all day searching the internet and trying ways, i have been unable to get anything working, could anyone give me a hand please?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2009
Added on Mar 19 2009
2 comments
406 views