Java ObjectOutputStream: Writing and Serializing Objects to File
🚀 What Will You Learn in This Guide?
In this guide, you will learn how to serialize objects and write them to a file using the Java ObjectOutputStream class.
We will also examine the concepts of the Serializable interface, transient keyword and serialVersionUID through a real example.
🧠 Technical Summary
Subject: Object serialization in Java.
Purpose: To write live objects in memory to a persistent medium (file, database, etc.) or transmit them over the network.
Area of Use: Data storage, backup, network transmission, data transfer in corporate applications.
⚙️ What is ObjectOutputStream?
ObjectOutputStream converts a Java object to a byte stream. This process is called serialization.
The serialized object can then be written to file or sent over the network.
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("CalisanObjesi.ser"));
🧩 This line creates an ObjectOutputStream to write objects to the file.
🧩 ObjectOutputStream Requirements The class to be serialized must implement the java.io.Serializable interface. This interface is a marker interface, that is, it does not contain any methods.
If the class is not Serializable:
java.io.NotSerializableException: ornek.com.veri.Calisan
hatası alınır.
✍️ Step by Step: Writing Object to File
1️⃣ Creating a Serializable Class
package ornek.com.veri;
import java.io.Serializable;
public class Calisan implements Serializable {
private static final long serialVersionUID = -299482035708790407L;
private String ad;
private String cinsiyet;
private int yas;
private String pozisyon;
public Calisan(String ad) { this.ad = ad; }
@Override
public String toString() {
return "Calisan:: Ad=" + ad + " Yas=" + yas +
" Cinsiyet=" + cinsiyet + " Pozisyon=" + pozisyon;
}
}
💡 This class is serializable and serialVersionUID ensures version compatibility.
2️⃣ Writing the Object to File (Serialization)
package ornek.com.veri;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamOrnek {
public static void main(String[] args) {
Calisan calisan = new Calisan("Aylin Yılmaz");
calisan.setYas(28);
calisan.setCinsiyet("Kadın");
calisan.setPozisyon("Proje Yöneticisi");
try (
FileOutputStream dosya = new FileOutputStream("CalisanObjesi.ser");
ObjectOutputStream nesneAkisi = new ObjectOutputStream(dosya);
) {
nesneAkisi.writeObject(calisan);
System.out.println("✅ Serileştirme işlemi tamamlandı!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
🧠 This code converts the Worker object into a byte array and writes it to the “CalisanObjesi.ser” file.
3️⃣ Transient Keyword
If you don't want some fields to be serialized, you can use transient.
private transient String pozisyon;
💡 This field is ignored during serialization.
4️⃣ serialVersionUID Importance
serialVersionUID ensures class version compatibility during serialization and deserialization operations. If this field is not present and you make changes to the class, old files may not be read.
💬 Frequently Asked Questions (FAQ)
- Why can't the file be read after serialization?
Because the file is not human readable. The content is in binary format. To read, reverse serialization must be done with ObjectInputStream.
- Why is the Serializable interface mandatory?
This interface informs the JVM that “This object can be serialized.” It is a sign of security.
- Are static variables serialized?
No. Static fields belong to the class, not the object. That's why it is not recorded.
- What does Transient do?
Prevents certain fields from being written during serialization.
- How do I read back the object?
Using ObjectInputStream, you read it with the readObject() method and do type conversion.
🧩 ObjectOutputStream Method Summary
| 🧩 Method | 💡 Description |
|---|---|
| writeObject(Object o) | Serializes the object and writes it to the stream. |
| flush() | Cleans up the flow and forces typing. |
| close() | Closes the stream. |
| reset() | Resets the serialization state. |
🏁 Conclusion
Java ObjectOutputStream is the safe and standard way to persist objects. Serialization is especially critical in data transfer, file saving, and enterprise applications.
☁️ You can try this feature immediately for secure data transfer in the applications you develop on GenixNode.
yaml

