Java Training: Input/Output
Using RandomAccessFile Class
// Updating a selected record in a file
/**
* This source example shows how to use RandomAccessFile for data update
* @@author Jeff Zhuk
*/
// Open a file with read/write ("rw") access
RandomAccessFile raf = new RandomAccessFile("myFile","rw");
int index = 13; // record number
int recLength = 10; // record length in bytes
String news = new String("no news");
// seek for a specified record number 13
raf.seek(index * recLength);
// update a record ( keep the same record length )
for(int = 0; i < recLength; i++)
{
raf.writeChar((i < news.length) ? news.charAt(i) : ' ');
}