문제

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