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.

We can help you better if you post the relevant code as well.

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


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.

Thank a lot. I will try that and see if it will work.

Its working now. Thank You death_oclock (",)

Be a part of the DaniWeb community

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