Monday, March 24, 2014
Today I  will show you "how to save objects to file" and "how to read objects from file". This is a very important thing which you may need while writing an application in Java. You might have an application where you would like to save the state of an object which you may require later. You can think of using databases, but it is very odd to use it for saving small number of objects. Also it may be that you dont have access to databases. So it is best to use the local file systemand save objects in files. This will make the application more light and have freater performance.
     This is very easy to do. Just as you write and read all other things from file, similarly this can be done. You have to do just these two things

  • The class whose object you want to save must  implement the interface java.io.Serializable. This interface is a tagging interface and has no abstract methods. As you knowobjects have existence only in JVM and they have no meaning in the external world. So making a class Serializable is quite like signing a contract with JVM that it can be taken outside it but it will be broken into bytes which will be actually saved in file. Similarly while reading the series of bytes from file will be read and the object will be reconstructed.
  • Wgile writing an object you need a stram to write it. The class java.io.ObjectOutputStream will help to write objects while java.io.ObjectInputStream will help to read objects.
-------------------------------------------------------------------------------------------------------------------------
Java Source Code
-------------------------------------------------------------------------------------------------------------------------
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class Student implements Serializable{
 private long roll;
 private String name;
 private static final long serialVersionUID = 1L;
 
 public Student(long roll, String name){
  this.roll = roll;
  this.name = name;
 }

 @Override
 public String toString() {
  return "Student [roll=" + roll + ", name=" + name + "]";
 }
}

class SerializableDemo{
 public static void main(String[] args) throws ClassNotFoundException, IOException {
     //creating instance of Student to save to file
  Student s = new Student(11004L,"Aditya Goyel");
  System.out.println("Before saving in file >>\n"+s);
  //creating the stream to write to file
  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("out.dat"));
  oos.writeObject(s);  //writing the object
  oos.close();  //closing the stream
  //creating the stream to read from file
  ObjectInputStream ois = new ObjectInputStream(new FileInputStream("out.dat"));
  Object o = ois.readObject();  //reading from file
  if(o instanceof Student)  //checking if it is a Student object
   o = (Student)o;  //type-cast to Student
  System.out.println("\nAfter reading from file >> \n"+o);
  ois.close();  //closing stream
 }
}
-------------------------------------------------------------------------------------------------------------------------
Output
-------------------------------------------------------------------------------------------------------------------------
Before saving in file >>
Student [roll=11004, name=Aditya Goyel]

After reading from file >>
Student [roll=11004, name=Aditya Goyel]

-------------------------------------------------------------------------------------------------------------------------
Download Links
-------------------------------------------------------------------------------------------------------------------------

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.