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]

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.