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.

Recommended Answers

All 5 Replies

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 {
        file.seek(l);

        f = k + "rewritten";

        file.writeBytes(f);

    }

    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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.