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!

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

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.

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>

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!

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

Edited 5 Years Ago by Ezzaral: Snipped email. Please keep it on site.

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.

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.

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);
		}
	}
}
This article has been dead for over six months. Start a new discussion instead.