Delete last line from text file

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 8
Reputation: VinceAshbySmith is an unknown quantity at this point 
Solved Threads: 0
VinceAshbySmith VinceAshbySmith is offline Offline
Newbie Poster

Delete last line from text file

 
0
  #1
Mar 19th, 2009
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:

  1. Writer output = null;
  2. File file = new File("playerData.xml");
  3. try {
  4. output = new BufferedWriter(new FileWriter(file, true));
  5. output.write("\t" + "<player>" + "\n");
  6. output.write("\t" + "\t" + "<playerName>" + playerName + "</playerName>" + "\n");
  7. output.write("\t" + "\t" + "<playerScore>" + pointCount + "</playerScore>" + "\n");
  8. output.write("\t" + "\t" + "<playerTime>" + minutes + " minutes " + seconds + " seconds" + "</playerTime>" + "\n");
  9. output.write("\t" + "</player>" + "\n");
  10. output.write("<indianaTuxPlayers>" + "\n");
  11.  
  12. output.close();
  13. System.out.println("Player data saved!");
  14. }
  15. catch (IOException e) {
  16. e.printStackTrace();
  17. }

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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Delete last line from text file

 
0
  #2
Mar 19th, 2009
read it and as you read it write it out to another file, but don't write the last line.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 8
Reputation: VinceAshbySmith is an unknown quantity at this point 
Solved Threads: 0
VinceAshbySmith VinceAshbySmith is offline Offline
Newbie Poster

Re: Delete last line from text file

 
0
  #3
Mar 19th, 2009
Ok could you give me a quick idea of how to do that, not sure as i haven't done much work in reading/writing files in java
Last edited by VinceAshbySmith; Mar 19th, 2009 at 2:36 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Delete last line from text file

 
0
  #4
Mar 19th, 2009
Is there a reason you're not working with the file as an xml document though a DOM api?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 8
Reputation: VinceAshbySmith is an unknown quantity at this point 
Solved Threads: 0
VinceAshbySmith VinceAshbySmith is offline Offline
Newbie Poster

Re: Delete last line from text file

 
0
  #5
Mar 19th, 2009
Yes as i have been unable to create a multi tree like structure using the DOM api, the best i can get is something like this:

<indianaTuxPlayers>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</indianaTuxPlayers>

and i want to add another level, so something like this:


<indianaTuxPlayers>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
<player>
</indianaTuxPlayers>

Plus each time the method is called it creates multiple versions of the data, so i get something like this:

<indianaTuxPlayers>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
<player>
</indianaTuxPlayers>
<indianaTuxPlayers>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
<player>
</indianaTuxPlayers>

Which when you try reading that, it throws up a whole heap of erros with regards to well formed xml.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Delete last line from text file

 
1
  #6
Mar 19th, 2009
Perhaps
  1. <team name="indianaTux">
  2. <player>
  3. <Name>Default Player</Name>
  4. <Score>0</Score>
  5. <Time>null minutes null seconds</Time>
  6. </player>
  7. <player>
  8. <Name>Another Player</Name>
  9. <Score>0</Score>
  10. <Time>null minutes null seconds</Time>
  11. </player>
  12. </team>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 8
Reputation: VinceAshbySmith is an unknown quantity at this point 
Solved Threads: 0
VinceAshbySmith VinceAshbySmith is offline Offline
Newbie Poster

Re: Delete last line from text file

 
0
  #7
Mar 19th, 2009
yes thats what i want, and i have since managed to get the three levels, so root, then child and then subchild. However the problem is each time the method is called, this is always repeated, so i end up getting something like this:

<indianaTuxPlayer>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
</indianaTuxPlayer>
<indianaTuxPlayer>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
</indianaTuxPlayer>
<indianaTuxPlayer>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
</indianaTuxPlayer>

Which when you try read that back in you get errors because its not well formed!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,515
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 523
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Delete last line from text file

 
1
  #8
Mar 19th, 2009
Use a DOM api like JAXP or JDOM.
Last edited by Ezzaral; Mar 19th, 2009 at 4:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: salmah is an unknown quantity at this point 
Solved Threads: 0
salmah salmah is offline Offline
Newbie Poster

Re: Delete last line from text file

 
0
  #9
Mar 25th, 2009
I too have the same problem. . If you have found the correct code to delete the last line of a file. . plz do mail the code to salma2611@gmail.com. .
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Delete last line from text file

 
0
  #10
Mar 25th, 2009
If this is really what you want, and has nothing to do with XML, then re-read the reply #1.

And to complete, you then delete the original and rename the new to the original.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 2275 | Replies: 12
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC