| | |
Delete last line from text file
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 8
Reputation:
Solved Threads: 0
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:
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!
Java Syntax (Toggle Plain Text)
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!
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
----------------------------------------------
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
•
•
Join Date: Feb 2009
Posts: 8
Reputation:
Solved Threads: 0
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.
<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.
Perhaps
Java Syntax (Toggle Plain Text)
<team name="indianaTux"> <player> <Name>Default Player</Name> <Score>0</Score> <Time>null minutes null seconds</Time> </player> <player> <Name>Another Player</Name> <Score>0</Score> <Time>null minutes null seconds</Time> </player> </team>
•
•
Join Date: Feb 2009
Posts: 8
Reputation:
Solved Threads: 0
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!
<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!
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
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. .
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.
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
----------------------------------------------
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
![]() |
Similar Threads
- how to delete the last line of the text file using c# (ASP.NET)
- read-delete lines from a text file (PHP)
- delete first line from a text file (C++)
- How to delete a row in text file? (Visual Basic 4 / 5 / 6)
- 10 line text file (Java)
Other Threads in the Java Forum
- Previous Thread: CONVERT
- Next Thread: open and read all files from folder
Views: 2275 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for Java
android api apple applet application apps arguments array arrays automation awt binary bluetooth businessintelligence busy_handler(null) card chat class classes client code collision component constructor database draw eclipse error event eventlistener exception file fractal game givemetehcodez graphics gui helpwithhomework html ide image input integer j2me jar java javafx javamicroeditionuseofmotionsensor javaprojects jmf jni jpanel jtree julia link linux list loop machine map method methods mobile netbeans newbie nls number object oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms socket sort sortedmaps sql string swing test textfield threads time transfer tree unlimited webservices windows






