The constructor for the ObjectOutputStream class requires a reference to a FileOutputStream object as an argument that defines the stream for the file where you intend to store your objects. You could create an ObjectOutputStream object with the following statements:
The object to be serialized must implement java.io.Serializable. The following example serializes a javax.swing.JButton object.
Object object = new javax.swing.JButton("submit"); try { ObjectOutput out = new ObjectOutputStream(new FileOutputStream("tempFile")); out.writeObject(object); out.close(); ByteArrayOutputStream bos = new ByteArrayOutputStream() ; out = new ObjectOutputStream(bos) ; out.writeObject(object); out.close(); byte[] buf = bos.toByteArray(); } catch (IOException e) { }
Writing Basic Data Types to an Object Stream
You can write data of any of the primitive types using the methods defined in the ObjectOutputStream class for this purpose. For writing individual items of data of various types, you have the following methods:
write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to the underlying output stream. write(int b) Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. writeBoolean(boolean v) Writes a boolean to the underlying output stream as a 1-byte value. writeByte(int v) Writes out a byte to the underlying output stream as a 1-byte value. writeBytes(String s) Writes out the string to the underlying output stream as a sequence of bytes. writeChar(int v) Writes a char to the underlying output stream as a 2-byte value, high byte first. writeChars(String s) Writes a string to the underlying output stream as a sequence of characters. writeDouble(double v) Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. writeFloat(float v) Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. writeInt(int v) Writes an int to the underlying output stream as four bytes, high byte first. writeLong(long v) Writes a long to the underlying output stream as eight bytes, high byte first. writeShort(int v) Writes a short to the underlying output stream as two bytes, high byte first. writeUTF(String str) Writes a string to the underlying output stream using Java modified UTF-8 encoding in a machine-independent manner.
None of them return a value and they can all throw an IOException since they are output operations.