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?

有帮助吗?

解决方案

I've found the solution. That class had a custom add() method that rewrote the file everytime something was added. At the beginning of the loop there were 100 entries, but after it executed once only one entry was left. There was also some extra code that had always added those missing 99 entries.

Replacing add() with super.add() solved the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top