Help!

Im trying to overwrite a certain record in a text file (basically a log in log out file).
Im using Random Access File.

I tried to write the updated record using writeBytes but what happens is that it was APPENDED after the record i wish to overwrite.

from
150.20.111.0&2010062216:06:31
150.20.111.0&2010062216:07:01
150.20.111.0&2010062216:07:17

to
150.20.111.0&2010062216:06:31 <----- ( i need to overrwite this not append)
150.20.111.0&2010062216:06:31rewritten <------ (with this)
150.20.111.0&2010062216:07:01
150.20.111.0&2010062216:07:17


thanks a lot.

This is my code. Thanks a lot.

public void search(){
    try {
        file = new RandomAccessFile("monitor.txt", "rws");
        h = "2010062216:06:31";

        while (!found) {
             k = file.readLine();
             l = file.getFilePointer();

             if (k == null) {return;}

             m = k.substring((k.indexOf('&') + 1), k.length());

             if (m.equals(h)){
             found = true;
             }
        }

    }
    catch (IOException ioe){}
}


public void rewriteLog(){
    try {
        file.seek(l);

        f = k + "rewritten";

        file.writeBytes(f);

    }

    catch (IOException ioe) {}

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I honestly don't know why the write operation is inserting instead of overwriting. I can point out that you should put l = file.getFilePointer(); BEFORE you you read the line because the read operation will move the pointer to after the line.

Edited 6 Years Ago by death_oclock: n/a

This question has already been answered. Start a new discussion instead.