title: Java Serialization - Recovering serialized file after process crash
body: <p>I have a following usecase.</p>
  
  <ul>
  <li>A process serializes certain objects to a file using <code>BufferedOutputStream</code>.</li>
  <li>After writing each object, process invokes <code>flush()</code></li>
  <li>The use case is that if the process crashes while writing an object, I want to recover the file upto the previous object that has been written successfully. </li>
  </ul>
  
  <p>How can I deserialize such file? How will Java behave while deserializing such file. </p>
  
  <ul>
  <li>Will it successfully deserialize upto the object that were written successfully before crash?</li>
  <li>While reading the last partially written object, what will be the behavior. How can I detect that?</li>
  </ul>
  
  <p><strong>Update1 -</strong> </p>
  
  <ul>
  <li>I have tried to simulate process crash via manually killing the process while objects are being written. I have tried around 10-15 times.Each time i am able to deserialize the file and file does not has any partial object.</li>
  </ul>
  
  <p>I am not sure if my test is exhaustive enough and therefore need further advice. </p>
  
  <p><strong>Update2  -</strong> Adam had pointed a way which could simulate such test using truncating the file randomly. 
  Following is the behavior observed for trying out around 100 iterations -</p>
  
  <ul>
  <li>From the truncated file ( which should be equivalent to the condition of file when a process crashes), Java can read upto last complete object successfully. </li>
  <li>Upon reaching the last partially written object, Java does not throw any <code>StreamCorruptedException</code> or <code>IOException</code>. It simply throws <code>EOFException</code> indicated <code>EOF</code> and ignores the partial object.</li>
  </ul>