954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Delete last line from text file

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 "" 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"?>
Default Player0null minutes null secondsDefault Player0null minutes null secondsDefault Player0null minutes null seconds

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!

VinceAshbySmith
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

read it and as you read it write it out to another file, but don't write the last line.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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

VinceAshbySmith
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Is there a reason you're not working with the file as an xml document though a DOM api?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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:

Default Player0null minutes null seconds

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

Default Player0null minutes null seconds

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

Default Player0null minutes null secondsDefault Player0null minutes null seconds

Which when you try reading that, it throws up a whole heap of erros with regards to well formed xml.

VinceAshbySmith
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Perhaps

<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>
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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:

Default Player0null minutes null secondsDefault Player0null minutes null secondsDefault Player0null minutes null seconds

Which when you try read that back in you get errors because its not well formed!

VinceAshbySmith
Newbie Poster
8 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Use a DOM api like JAXP or JDOM .

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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 [snipped]. .

salmah
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

While you do not need to use the DOM API to handle the XML, it is recommened. Just going through and deleting the last line and appending what you think is right can result in poorly formed XML documents.

If you are having troubles understanding the DOM API please feel free to post your code and ask questions, there are plenty of us here to help.

The basic idea of working with DOM is to:

Get your Document
Load your XML into it
Manipulate/create the tree
Save the document

Adding / removing nodes is very easy and you can build a tree as deep as you need.

Killer_Typo
Master Poster
781 posts since Apr 2004
Reputation Points: 152
Solved Threads: 39
 

You don't need to use DOM API for something as simple as this. Here is the code that you can use directly. I've implemented using RandomAccessfile.

import java.io.*;
public class PlayerAdder
{
	public static void main(String args[]) {
		String playerName = "John" ;
		String pointCount = "10";
		String minutes	= "30";
		try{
			RandomAccessFile rand = new RandomAccessFile("playerData.xml","rw");
			long OldLength = rand.length();
			long NewLength = OldLength - 20;
			rand.setLength(NewLength);
			rand.seek(rand.length());
			///adding tags and their data
			rand.writeBytes("\n");
			rand.writeBytes("\t");
			rand.writeBytes("<player>");
			rand.writeBytes("\n");
			rand.writeBytes("\t");
			rand.writeBytes("\t");
			rand.writeBytes("<playerName>");
			rand.writeBytes(playerName);
			rand.writeBytes("</playerName>");
			rand.writeBytes("\n");
			rand.writeBytes("\t");
			rand.writeBytes("\t");
			rand.writeBytes("<pointCount>");
			rand.writeBytes(pointCount);
			rand.writeBytes("</pointCount>");
			rand.writeBytes("\n");
			rand.writeBytes("\t");
			rand.writeBytes("\t");
			rand.writeBytes("<minutes>");
			rand.writeBytes(minutes);
			rand.writeBytes("</minutes>");
			rand.writeBytes("\n");
			rand.writeBytes("\t");
			rand.writeBytes("</player>");
			rand.writeBytes("\n");
			rand.writeBytes("</indianaTuxPlayer>");
			rand.close();
		}
		catch(Exception e)
		{
			System.out.println("Exception occured :"+e);
		}
	}
}
camocspro
Newbie Poster
4 posts since Dec 2009
Reputation Points: 10
Solved Threads: 2
 

:sigh: double :sigh:

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You