Object Input output in java - Object serializable


serializable is the interface in java which is used to make an object capable of being stored and retrieved to and from a file. It requires the ObjectInputStream and ObjectOutputStream, learn what is serialization and where it is used.

In this one we are going to explain the Object Input Output. The object IO is known as the serialization and De-serialization of the object. In this post we will tell you how you can save the objects in the file and retrieve them back when you need them.

Searializable

The Serializable is an interface provided by Java it does not contains any method to override but it is necessary to implement this Interface in the class whose object you want to store or retrieve to or from the file. The serialization is done using the Output Stream and Input Stream. There are two classes, ObjectOutputStream and ObjectInputStream, which are used for the purpose.

Following program will demonstrate the use of Object Output and Object Input Stream.


import java.io.*;

public class ObjectIO implements Serializable{
	public static void main(String[] ExamsMyantra) throws IOException,ClassNotFoundException{
		ObjectIO obj = new ObjectIO();
		String s = "hello world";
		try{
			FileOutputStream fos = new FileOutputStream("object.txt");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(obj);
			oos.writeObject(s);

			FileInputStream fis = new FileInputStream("object.txt");
			ObjectInputStream ois = new ObjectInputStream(fis);
			ObjectIO obj1 = (ObjectIO)ois.readObject();
			System.out.println(obj1.toString());
			String s1 = (String)ois.readObject();
			System.out.println(s1.toString());
		}
		catch(IOException ioe){
			System.out.println(ioe);
		}
	}
}

In this program what we have done is, Created an object of the current class that is, ObjectIO and another class, String which implements the serializable internally by the Java language specification. After that we have created a FileInputStream which will create a file and then we will wrap it into the Object output Stream. Well, preparations are now complete and you can write in the object into the file. Which is done using the method writeObject().

this will create a file named object.txt and will write the object, if you will try to look into file you'll find the garbage which is unreadable to the humans as you can see in following image.

File Object

Well not to worry, Now if you want to check out how if these objects were actually written in the file, you must read them using ObjectInputStream. as shown in the further code lines. The FileInputStream is created to be wrapped in ObjectInputStream. The object is read in using the method readObject(), which returns a Object type reference which you must cast into the required object type.

As we show in the output of this program here

ObjectIO@2d23fee1
hello world

The object obj1 is output in the form of String so it is shown like this, but the object String is actually the string to invoking toString() on this would not effect anything. We have placed only to demonstrate the purpose, this is optional and you may remove it, It would not effect the program.

Note: The Objects must be read in the same order in which they were written in the file, other wise the program will produce the Exception.

To certify the above mentioned note we would like to demonstrate you the result. All we have done in the code is this little change, calling the readObject on string first.


String s1 = (String)ois.readObject();
System.out.println(s1.toString());
ObjectIO obj1 = (ObjectIO)ois.readObject();
System.out.println(obj1.toString());

and program produced following exception on runtime

Exception in thread "main" java.lang.ClassCastException: ObjectIO cannot be cast to java.lang.String
	at ObjectIO.main(ObjectIO.java:15)

You see, beside this you are good to go with this code. However we have another good suggestion for you if you want multiple objects to be serialize. You can store the object references in the ArrayList and store that List in the serialize form in this way the system would have to care for storing only one object, that is ArrayList and rest of your object will remain safe inside that ArrayList object.