Mega Code Archive

 
Categories / Java Book / 004 File Stream
 

0247 ObjectOutputStream class

The ObjectOutputStream class extends the OutputStream class and implements the ObjectOutput interface. It is responsible for writing objects to a stream. A constructor of this class is ObjectOutputStream(OutputStream outStream) throws IOException outStream is the output stream to which serialized objects will be written. void close( ) Closes the invoking stream. void flush( ) Flushes the output buffers. void write(byte buffer[ ]) Writes an array of bytes to the invoking stream. void write(byte buffer[ ], int offset, int numBytes) Writes a subrange of numBytes bytes from the array buffer, beginning at buffer[offset]. void write(int b) Writes a single byte to the invoking stream. The byte written is the low-order byte of b. void writeBoolean(boolean b) Writes a boolean to the invoking stream. void writeByte(int b) Writes a byte to the invoking stream. The byte written is the low-order byte of b. void writeBytes(String str) Writes the bytes representing str to the invoking stream. void writeChar(int c) Writes a char to the invoking stream. void writeChars(String str) Writes the characters in str to the invoking stream. void writeDouble(double d) Writes a double to the invoking stream. void writeFloat(float f ) Writes a float to the invoking stream. void writeInt(int i) Writes an int to the invoking stream. void writeLong(long l) Writes a long to the invoking stream. final void writeObject(Object obj) Writes obj to the invoking stream. void writeShort(int i) Writes a short to the invoking stream. Revised from Open JDK source code import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.Date; public class Main { public static void main(String args[]) throws IOException { FileOutputStream fos = new FileOutputStream("t.tmp"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject("Today"); oos.writeObject(new Date()); oos.close(); } }