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!

Recommended Answers

All 12 Replies

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

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

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

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!

Use a DOM api like JAXP or JDOM.

commented: agreed, no reason to not use the API. +7

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

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);
		}
	}
}

:sigh: double :sigh:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.