title: Reading Objects from Random Access File
body: <p>I wrote a file using Java's FileChannel class that uses RandomAccessFiles. I wrote objects at various locations in the file. The objects were of variable sizes but all of the same class. I wrote the objects using the following idea :</p>
  
  <p><CODE><pre>ByteArrayOutputStream bos= new ByteArrayOutputStream();
          ObjectOutput out = new ObjectOutputStream(bos);
          out.writeObject(r);
          byte[] recordBytes= bos.toByteArray();</p>
  
  <pre><code>    ByteBuffer rbb= ByteBuffer.wrap(recordBytes);
  
      while(rbb.hasRemaining()) {
          fileChannel.write(rbb);
      }
  </code></pre>
  
  <p></pre>
  </code></p>
  
  <p>Now I want to read from such a file. I dont want to have to specify the number of bytes to read. I want to be able to read the object directly using Object Input Stream. How to achieve this ? </p>
  
  <p>I <em>have</em> to use Random Access Files because I need to write to different positions in file. I am also recording in a separate data structure, the locations where objects have been written.</p>