What is the best way to fully read a stream of objects from a file in Java?

I am creating a potentially long log of objects and do not want to store them all in memory before writing to a file, so I cannot write a serialized collection of objects to a file. I am trying to figure out the "best" way to read the entire stream of objects after registration is complete.

I noticed that the following does not work:

FileInputStream fis = new FileInputStream(log);
ObjectInputStream in = new ObjectInputStream(fis);
while ((obj = in.readObject()) != null) {
  // do stuff with obj
}

because the stream throws an exception when it reaches the end of the file rather than returning zero (presumably because you can write / read zero streams of objects, as a result of which the above loop will not behave as expected).

Is there a better way to do something like what I want to accomplish using the above loop than:

FileInputStream fis = new FileInputStream(log);
ObjectInputStream in = new ObjectInputStream(fis);
try {
  while (true) {
    obj = in.readObject();
    // do stuff with obj
  }
} catch (EOFException e) {
}

. ?

private static final class EOFObject implements Serializable {
  private static final long serialVersionUID = 1L;
}

void foo() {
  Object obj;
  while (!((obj = in.readObject()) instanceof EOFObject)) {
    BidRequest bidRequest = ((BidRequestWrapper) obj).getBidRequest();
    bidRequestList.add(bidRequest);
  }
}
+3
4

. , finally, .

EOF- . , , EofObject break .

+5

,

Java, , -, , , . , :

public static void main(String[] args) throws Exception {
    OutputStream os = new FileOutputStream("C:\\test");
    ObjectOutputStream oos = new ObjectOutputStream(os);
    for (Integer i = 0; i < 1E9; i++) {
        oos.writeObject(i);
    }
    oos.close();
}

. , , , , .

, , ObjectOutputStream () ObjectOutputStream.reset() - , . OutOfMemoryError:

public static void main(String[] args) throws Exception {
    OutputStream os = new FileOutputStream("C:\\test");
    ObjectOutputStream oos = new ObjectOutputStream(os);
    for (Integer i = 0; i < 1E9; i++) {
        oos.writeObject(i);
        oos.reset();
    }
    oos.close();
}

, reset, ( 80 ...), reset , , 100 ?

, bozho EOF.

+3

boolean , false. , , :

true
<object>
true
<object>
true
<object>
false

Then, reading them back, you check the flag (you know that there will always be one after each object) to decide whether to read or not.

boolean will be stored very compactly in the serialization stream, so it should not greatly increase the file size.

+2
source

Your code is incorrect. readObject () does not return null in EOS, it throws an EOFException. So get it. Null is returned if you wrote zero. You do not need all the logical or marker objects suggested above.

0
source

Source: https://habr.com/ru/post/1793134/


All Articles