<<< RandomAccessFile example: writing binary file | Index | RandomAccessFile example: fixed-length string I/O >>> |
Code that reads data from a file
final int RECORD_LENGTH = 16; // 4 chars @ 2 bytes per
// char +1 double @ 8
// bytes
RandomAccessFile booksFile =
new RandomAccessFile("books.ran", "r");
// move the pointer to the third record
int recordNumber = 3;
booksFile.seek((recordNumber - 1) * RECORD_LENGTH);
// read the third record
String code = "";
for (int i = 0; i < 4; i++)
code += booksFile.readChar();
double price = booksFile.readDouble();
booksFile.close();
<<< RandomAccessFile example: writing binary file | Index | RandomAccessFile example: fixed-length string I/O >>> |