Hi all wonder if someone could give me a bit of help, i'm new to using RnadomAccessFiles, so please bear with me. Basically i have the method below, which writes some data into a xml file, what i want to be able to do, is move the file pointer to back 1 each time. So in other words, each time the method is called, it will delete the last line of the previous entry.

So each time the method is called it will produce the following output:

<?xml version="1.0" encoding="UTF-8"?>
<indianaTuxPlayers>
<player>
<playerName>Default Player</playerName>
<playerScore>0</playerScore>
<playerTime>null minutes null seconds</playerTime>
</player>
</indianaTuxPlayers>

What i want to be able to do is delete "</indianaTuxPlayers>" each time the method is called and then write the rest of the data.

public void writeData(){
		 try {
		RandomAccessFile raf = new RandomAccessFile("test.xml", "rw");

	            String text[] = new String[2];
	            text[0] = "<?xml version='1.0' encoding='UTF-8'?>";
	            text[1] = "<indianaTuxPlayers> + '\n'";


	            for (int i = 0; i < text.length; i++) {
	                raf.writeUTF(text[i]);
	            }

	            //
	            // Write another data at the end of the file.
	            //
	            raf.seek(raf.length());
		        raf.writeUTF("\t" + "<player>" + "\n");
		        raf.writeUTF("\t" + "\t" + "<playerName>" + playerName + "</playerName>" + "\n");
		        raf.writeUTF("\t" + "\t" + "<playerScore>" + pointCount + "</playerScore>" + "\n");
		        raf.writeUTF("\t" + "\t" + "<playerTime>" + minutes + " minutes " + seconds + " seconds" + "</playerTime>" + "\n");
		        raf.writeUTF("\t" + "</player>" + "\n");
		        raf.writeUTF("</indianaTuxPlayers>" + "\n");
		        raf.close();

	            //
	            // Move the file pointer to the beginning of the file
	            //
	            //raf.seek(0);
		    } catch (IOException e) {}
}

You need to check how long the last line is before you write it, then use seek to back up that amount.

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.