I'm writing to a RandomAccessFile
like that: (in a LinkedList's subclass)
file.setLength(0);
for (Person person : this)
file.writeUTF(person.getBlob());
Person.getBlob()
returns a string of constant length, containing only basic alphanumeric characters, spaces and CRs (only one-byte characters). At this place the file contains exactly 100 records. (confirmed with a hex editor)
Then I try to read that file:
int counter = 0;
while (true) {
try {
add(Person.fromBlob(file.readUTF()));
} catch (EOFException e) {
System.out.println(counter + " records read from file.");
break;
} catch (Exception exception) {
throw new DBException(exception);
}
counter++;
}
I always end up with one record read correctly and an EOFException
. What's wrong with this code?