Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com

Essence.

The only class that allows updating a file at 'random' byte positions is java.io.RandomAccessFile.

Upon creation you tell it, you want to be able to read AND write :

As you can see, we're using the same file as before.

The first data item, an int, is read and ignored.

Save the file pointer.

Then a double is read and stored :

We add 10% to it :

The file-pointer is now pointing at the first String, so we reverse it to the first byte of the double.

Then we write the changed value :

As you can imagine, POSITIONING THE FILE-POINTER CORRECTLY is crucial to this technique !

Updating strings is tricky, since you would have to use a fixed length string, padding where necessary, to make such a file a bit workable.

In any case, for using RandomAccessFile succesfully it's easiest to use a fixed record layout, so your file is guaranteed to contain a repeating series of f.e. our int, double, String, String, int, double, String, String, etc.

Matters are greatly simplified if you create a record class with read/write methods, file pointer position(s) and what else you can think of.

.