
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - ObjectOutputStream defaultWriteObject() method
Description
The Java ObjectOutputStream defaultWriteObject() method writes the non-static and non-transient fields of the current class to this stream. This may only be called from the writeObject method of the class being serialized. It will throw the NotActiveException if it is called otherwise.
It's used inside a custom writeObject() method of a class that implements Serializable.
It tells Java to perform default serialization for all non-transient fields.
You can then add custom data after the default fields.
Declaration
Following is the declaration for java.io.ObjectOutputStream.defaultWriteObject() method.
public void defaultWriteObject()
Parameters
NA
Return Value
This method does not return a value.
Exception
IOException − If I/O errors occur while writing to the underlying OutputStream.
Example - Usage of ObjectOutputStream defaultWriteObject() method
The following example shows the usage of ObjectOutputStream defaultWriteObject() method.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectOutputStreamDemo { public static void main(String[] args) { int i = 319874; try { // create a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeInt(i); oout.writeInt(1653984); oout.flush(); // close the stream oout.close(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // read and print an int System.out.println("" + ois.readInt()); // read and print an int System.out.println("" + ois.readInt()); } catch (Exception ex) { ex.printStackTrace(); } } static class Example implements Serializable { String s = "Hello World!"; private void writeObject(ObjectOutputStream out) throws IOException, ClassNotFoundException { out.defaultWriteObject(); } } }
Output
Let us compile and run the above program, this will produce the following result −
319874 1653984
Example - Usage of ObjectOutputStream defaultWriteObject() method
The following example shows the usage of ObjectOutputStream defaultWriteObject() method.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectOutputStreamDemo { public static void main(String[] args) throws Exception { String file = "user1.ser"; // Serialize the object try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { User user = new User("Alice", "secret123"); oos.writeObject(user); } System.out.println("User object serialized."); } static class User implements Serializable { private static final long serialVersionUID = 1L; String username; transient String password; // transient field won't be serialized by default User(String username, String password) { this.username = username; this.password = password; } // Custom serialization private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); // writes non-transient fields oos.writeUTF(password); // manually write the transient field } } }
Output
Let us compile and run the above program, this will produce the following result−
User object serialized.
Explanation
username is serialized normally via defaultWriteObject().
password is transient, so we manually write it after.
Example - Usage of ObjectOutputStream defaultWriteObject() method
The following example shows the usage of ObjectOutputStream defaultWriteObject() method.
ObjectOutputStreamDemo.java
package com.tutorialspoint; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; public class ObjectOutputStreamDemo { public static void main(String[] args) throws Exception { String file = "data2.ser"; // Serialize the object try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { Data d = new Data("Hidden message"); oos.writeObject(d); } System.out.println("Data object serialized."); } static class Data implements Serializable { private static final long serialVersionUID = 1L; String message; Data(String message) { this.message = message; } // Custom serialization without defaultWriteObject private void writeObject(ObjectOutputStream oos) throws IOException { // intentionally NOT calling defaultWriteObject() oos.writeUTF("Encrypted: " + message); } } }
Output
Let us compile and run the above program, this will produce the following result−
Data object serialized.
Explanation
Skipping defaultWriteObject() means default fields like message won't be written unless you handle them manually.
This gives full control over what's saved, but you must also write a custom readObject() to read it back.