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.


to <----- ( i need to overrwite this not append) <------ (with this)

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 {;

        f = k + "rewritten";



    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.