Java Reference ยท I/O and Files
ObjectOutputStream and ObjectInputStream
Reference for ObjectOutputStream and ObjectInputStream: write and read object graphs, transient fields, shared references, and the security risks of deserialization.
ObjectOutputStream converts a graph of Java objects into a stream of bytes, and ObjectInputStream rebuilds the objects from those bytes. This is Java's built-in serialization. Only objects whose class implements Serializable can be written; everything the object refers to is written too, unless it is transient or static.
Native serialization is convenient but fragile and risky. It ties the byte format to your class structure, and reading serialized data from an untrusted source can execute attacker-chosen code paths. For storage and APIs most projects choose JSON, Protocol Buffers or a database instead, and keep Java serialization for trusted, short-lived data such as caches or sessions in the same application.
try (ObjectOutputStream out = new ObjectOutputStream(stream)) { out.writeObject(order); } Order copy = (Order) new ObjectInputStream(input).readObject();
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
ObjectOutputStream(OutputStream out) throws IOException void writeObject(Object obj) throws IOException void reset() throws IOException ObjectInputStream(InputStream in) throws IOException Object readObject() throws IOException, ClassNotFoundException void setObjectInputFilter(ObjectInputFilter filter)
| Member | Description |
|---|---|
writeObject(obj) |
Writes the object and everything reachable from it. Shared references are written once. |
writeInt, writeUTF, ... |
ObjectOutputStream implements DataOutput, so primitives can be mixed in. |
reset() |
Forgets objects already written, so they are written again in full. |
readObject() |
Reads the next object; you must cast it. |
setObjectInputFilter(filter) |
Accept or reject classes, depth and array sizes while reading (Java 9+). |
Throws: NotSerializableException (class or field not serializable), InvalidClassException (class version mismatch), StreamCorruptedException, ClassNotFoundException, EOFException, and other IOExceptions.
Usage notes
- Never deserialize untrusted data. Crafted streams can trigger dangerous code in classes on your classpath ("gadget chains"). If you must, install an allow-list filter (
ObjectInputFilter.Config.createFilter("com.myapp.*;!*")) or set a process-wide filter. - Constructors do not run for serializable classes when reading; the first non-serializable superclass constructor does. Fields are restored directly, so invariants checked in constructors can be bypassed.
transientandstaticfields are skipped and come back as0,falseornull.- Object graphs are preserved: shared references and cycles are rebuilt as written (
copy.home == copy.work). - Same object, written twice, is stored once; later changes to it are not written unless you call
reset(). Long-running streams that write the same objects can also leak memory, because the stream keeps a reference to every object it has seen. - Compatibility is controlled by
serialVersionUID; see Serializable. - Header: the
ObjectInputStreamconstructor reads a header and blocks until it arrives. - Records, enums are serialized by their components or constant name, and the constructors of a record do run on reading.
- Alternatives: JSON libraries such as Jackson are text-based, language-neutral, and easier to evolve.