dantinkakkar 19 Junior Poster

Alright, so I tried to write a little short guide on Object Input and Output to files, since the worst situation one can find themselves is being unable to serialize files in a proper and readable format. One way to do so is to create an object of a class containing variables you need and then serialize it. The Java Library provides two functions, namely readObject() and writeObject() for the following purpose. Now, let us assume that I want to create a library-sort-of-system, where I want to save the book's name and author, and then I want to store the names of all of the books in a serialized [not the Java-term, the other orthodox term] manner, starting from 0 to n, I would have used a database. But since I'm explaining a file-based concept here, I'll use files; what I want to say is that a library management system is not a practical use and is only an example.

So, I have a table of this sort which I want to serialize...

BOOK_S.NO | BOOK AUTHOR | BOOK NAME
1 | Someone1 | Somename1
2 | Someone2 | Somename2

In a file-based system, we may define a class, say Booklist and then serialize it. Booklist may look like this:-

public class booklist
{
java.util.List book_auths = new java.util.ArrayList();
java.util.List book_names = new java.util.ArrayList();
/*Def. of an arraylist http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html*/
}

Other Notes 1. The readObject() and writeObject() methods have to be called from the object of an output/input stream and have to be saved to an Object variable. 2. http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html 3. http://docs.oracle.com/javase/tutorial/essential/io/bytestreams.html

The links provided will explain the concept further and you will be able to visualize the concept properly.

Happy Programming! And please give some feed [I'm new here, sorry from my side for any mistakes I've made above. Feel free to correct me.]

Cheers! :)

[image from oracle's website]

Dani AI

Generated

Nice primer by . The example gets the idea across; the notes below fill common gaps and highlight practical choices and failure modes that appear when object data is persisted to files.

Prefer a single Book type instead of parallel lists, and use typed collections and proper class naming. A minimal serializable POJO looks like this:

import java.io.Serializable;

public class Book implements Serializable {
    private static final long serialVersionUID = 1L;
    private String author;
    private String title;
    // getters/setters, equals/hashCode as needed
}

Common problems and quick fixes:

  • NotSerializableException — some member is not serializable. Mark transient, make that type serializable, or store a serializable surrogate.
  • InvalidClassException — class layout changed and the stored UID differs. Define an explicit serialVersionUID for controlled compatibility.
  • ClassNotFoundException — the reading code cannot find the class version used when the file was written; keep class names/package stable or provide migration code.
  • Stream corruption or EOF — mixing raw byte writes and object writes, or appending objects without handling headers. Write a single collection atomically or use a header-suppressing approach when appending.
  • Resource/flush issues — always close streams (try-with-resources) and test round-trip write/read on each change.

Design and long-term strategy:

  • Java built-in binary serialization is convenient for short-lived, internal storage. For long-term persistence, interchange, or cross-language needs, prefer explicit formats (JSON, CSV, protobuf) to reduce brittle coupling.
  • Add a small file header or version number to persisted files so migrations are explicit.
  • Never deserialize untrusted input; Java object deserialization can be a security risk.

Concise checklist: model data as plain POJOs, add serialVersionUID, test read/write after code changes, handle non-serializable members, and pick a stable format for long-term storage.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.