Hi,

At the moment my program holds a list video titles in a doubly linked list. When the user wants to save the list to file it is converted to an array then saved onto file.

But the problem starts when i want to delete from the list. Say for example there are two video titles in the linked list:

"Finding Nemo
Scarface."

if the user deletes the first video ie Finding Nemo, it will be correctly done in the linked list and also saved correctly on to file as:
"Scarface"

Then the user will close the program (and also the file). However, when they open it the contents on the file will be shown as:
"Scarface
Scarface".

I know it must be my file handling (and/or reading from file) that is dodgy but i can't understand why.

I am using RAF for handling the file and what *should* happen when i save to file is:
before saving anything to file, the file (called vidfile) is deleted.
then the file pointer is set back to the first position in the file and then it writes the array, videoarray, to vidfile.
Below is what my code looks like:
------------------------------------------------------------

RandomAccessFile raf = new RandomAccessFile(vidfile, "rw");
vidfile.delete();
raf.seek(0);
for(int x=0; x<videoarray.length; x++){
     videotitle=videoarray[x];
     raf.writeBytes(videotitle);
     System.out.println("This Video is now on file:\n"+ videotitle+"\n");
}

----------------------------------------------------

Can anyone help please?

Regards,
Nikki

If you insist on using RandomAccessFile (which I don't suggest), then call setLength(0) right after the seek.

However, since you're reading the entire file, then operating on the data, then writing the entire file, use a FileReader to read it (assuming simple text), then close that, and use a FileWriiter to write it, and then close that.

RandomAccessFile is meant mainly for use with datafiles that contain fixed-width data fields, not as a general purpose "file worker".

call setLength(0) right after the seek.

Thank you very much! It works! Thanks again!

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.