This code does what you want... you should read the sun docs on the ObjectOutputStream object here:
http://java.sun.com/j2se/1.4.1/docs/...putStream.html
Code:
import java.util.*;
import java.io.*;
public class ObjOut {
public static void main(String[] args) throws Exception {
// create some object
ArrayList list = new ArrayList();
list.add("One");
list.add("Two");
list.add("Three");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(list);
RandomAccessFile raf = new RandomAccessFile(
new File("./list"), "rw"
);
raf.write(bos.toByteArray());
raf.close();
}
}
Of course the code could be made more efficient by constructing the ObjectOutputStream with a FileOutputStream, instead of buffering the whole object in memory then writing it through the RandomAccessFile, but that's what you asked for.
HTH
B.