Friday, July 19, 2013

ObjectInputStream vs. ObjectOutputStream

ObjectInputStream is used to recover primitive data types and objects from a binary file. This process generally called deserialization. The primitive data types and objects can be written to the file by using the ObjectOutputStream class. We call this serialization. It is worth to note that an object that can be written to the file must be created from a class that implements the Serializable interface.

ObjectInputStream contructor:
-ObjectInputStream(FileInputStream fis)

Its Useful methods:
-readByte(byte[] barray):int
-readByte():byte
-readInt():int
-readLong():long
-readDouble():double
-readBoolean():boolean
-readChar():char
-readObject():Object
-readUTF():String


ObjectOutputStream constructor:
-ObjectOutputStream(FileOutputStream fos)

Its useful methods:
-writeByte(byte b):void
-writeBytes(String s):void
-writeInt(int i):void
-writeLong(long l):void
-writeDouble(double d):void
-writeChar(char c):void
-writeBoolean(boolean bo):void
-writeObject(Object obj):void
-writeUTF(String su):void

In the example below, the Student class is defined. It implements the Serializable interface so that its object can be written to the disk. Each student contains id, name, sex, dob, address, and phone information. It has only one constructor that allows you to construct a Student object and pass those information to the object. The writeObject method is called to write a Student object to a file that that its path is input by the user. To write an object to the file, you need to create the ObjectOutputStream object. This object takes the FileOutputStream object as is argument. Then the writeObject(Object obj) is invoked to write the object to the destination file. When reading the object back from the file, you need to user the ObjectInputStream class. Its constructor accept the FileInputStream object as its argument. The readObject method has to be used to get the object from the specified file. You have to use variable to store this object.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Student implements Serializable{
String id;
String name;
String sex;
String dob;
String address;
String phone;
public Student(String i, String n, String s, String d, String a, String p){
id=i;
name=n;
sex=s;
dob=d;
address=a;
phone=p;
}
public void showInfo(){
System.out.println(id+"\t"+name+"\t"+sex+"\t"+dob+"\t"+address+"\t"+phone);

}
}


public class ObjectTest{

public static void main(String[] args){

Student st=new Student("s001","Sok","M","02-06-1988","PP","012233333");
writeObj("D:/myprogram/student.bin",st);
readObj("D:/myprogram/student.bin");

}

public static void readObj(String src){
try{
FileInputStream fis=new FileInputStream(src); //create file inputstream
ObjectInputStream ois=new ObjectInputStream(fis); //create object input stream
Student student=(Student)ois.readObject(); //read object from the file
student.showInfo();
ois.close();
}catch(Exception e){e.printStackTrace();}

}
public static void writeObj(String src,Student st){
try{
FileOutputStream fos=new FileOutputStream(src); //create file outputstream
ObjectOutputStream oos=new ObjectOutputStream(fos);//create object output stream
oos.writeObject(st); //write object to the file
oos.flush();
oos.close();

}catch(Exception e){e.printStackTrace();}
}
}

No comments:

Post a Comment