Java Programing laungage

Java Input Output Projects

Java I/O Project 1

Java Input Output Examples

Java Input Output Examples

Object Stream

If you want to store the same type data then need to used the fixed-size record format. however , the object which we are create in the program is rarely same type.

Previous Home Next
adplus-dvertising

Example

Suppose that we are have an array called student, which is normally an array of the student record. but it is contain the object that is a instances of the child class such as Manage.

If we want to save the file, which is stored information, then we are save the object firstly and then the data that is define the current state of the object. When we are reading this information back from a file. you must read the object type; create the blank object of that type and fill it with the data, which is stored in the file;

Store Object of variable type

There are many steps for saving data in variable type:

  • we are open the ObjectOutputStream object.
    
    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("student.dat"));
    
    
  • Save the object with use of the writeObject() method which is included into the ObjectOutputStream class.
    
    student sam=new student("sam",100,1987,03,29);
    out.writeObject(sam);
    out.writeObject(senior);
    
    
  • For read the object in back in, get the ObjectInputStream object;
    
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("student.dat"));
    
    
  • Then retrieved the object in the same order with help of the readObject() method.
    
    student s1=(student)ois.readObject();
    student s2=(student)ois.readObject();
    
    

if you are reading the back object, then you are carefully keep the track number of object which is saved their order and types. all call to the readObject() method for read another object type.

Previous Home Next