
- 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 putFields() method
Description
The Java ObjectOutputStream putFields() method retrieves the object used to buffer persistent fields to be written to the stream. The fields will be written to the stream when writeFields method is called. putFields method is used inside a custom writeObject() method to manually define what fields should be serialized - instead of relying on default serialization.
It gives you an ObjectOutputStream.PutField object.
You can explicitly set values for the fields you want to serialize.
It allows partial control of what gets written to the stream, useful in versioning or for skipping fields.
Declaration
Following is the declaration for java.io.ObjectOutputStream.putFields() method.
public ObjectOutputStream.PutField putFields()
Parameters
NA
Return Value
This method returns an instance of the class Putfield that holds the serializable fields.
Exception
IOException − If an I/O error has occurred.
Example - Usage of ObjectOutputStream putFields() method
The following example shows the usage of ObjectOutputStream putFields() 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); // flush the stream 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 public class Example implements Serializable { private void writeObject(ObjectOutputStream out) throws IOException { // write into the ObjectStreamField array the variable string ObjectOutputStream.PutField fields = out.putFields(); } } }
Output
Let us compile and run the above program, this will produce the following result −
319874 1653984
Example - Custom serialization using putFields() to exclude a field
The following example shows the usage of ObjectOutputStream putFields() 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) throws IOException, ClassNotFoundException { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("put1.ser"))) { Person person = new Person("Alice", 30); oos.writeObject(person); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("put1.ser")); Person p1 = (Person)ois.readObject(); System.out.println("Name= " + p1.name + " age=" + p1.age); } } static class Person implements Serializable { private static final long serialVersionUID = 1L; String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } private void writeObject(ObjectOutputStream oos) throws IOException { ObjectOutputStream.PutField fields = oos.putFields(); fields.put("name", name); // Only write name // Skip age oos.writeFields(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Name= Alice age=0
Explanation
We only serialize the name field and intentionally exclude age. That is why, after de-serialization, output shows 'age=0'.
This is useful when you want to deprecate a field or hide it from future serialized output.
Example - Version-tolerant serialization - write extra placeholder field
The following example shows the usage of ObjectOutputStream putFields() 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) throws IOException, ClassNotFoundException { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("put2.ser"))) { Book book = new Book("Java Basics"); oos.writeObject(book); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("put2.ser")); Book b1 = (Book)ois.readObject(); System.out.println(" New field 'version' added in writeObject. After deserialization, 'version' = " + b1.version); } } static class Book implements Serializable { private static final long serialVersionUID = 1L; String title; int version = 2; // newer field public Book(String title) { this.title = title; } private void writeObject(ObjectOutputStream oos) throws IOException { ObjectOutputStream.PutField fields = oos.putFields(); fields.put("title", title); fields.put("version", version); // manually add new field oos.writeFields(); } } }
Output
Let us compile and run the above program, this will produce the following result−
New field 'version' added in writeObject. After deserialization, 'version' = 2
Explanation
We manually add a new version field for backward/forward.