Showing posts with label read Object from file. Show all posts
Showing posts with label read Object from file. Show all posts

Read and Write Objects from a file

Sometimes we may thinking of writing Objects into file and reading from other class or may need for future purpose. Basically we need to store the state of Object in a file and need to retrieve. For this we need to use Serializable interface to Serialize all class variables and by using ObjectInputStream and ObjectOutputStream class can be read and write from a file. Lets see simple example how to Serialize Object and same Object can to written into file. Next by another Java file we will read same Object from the file.
Read and Write Objects from a file



import java.io.Serializable;

public class Student implements Serializable {

 private static final long serialVersionUID = 1L;

 private String name;
 private int marks[] = new int[5];

 public Student(String name, int marks[]) {
  this.name = name;
  this.marks = marks;
 }

 public String getName() {
  return name;
 }

 public int[] getMarks() {
  return marks;
 }
}



In next program we will create Student Object and write to a file. 


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class WriteToFile {

 public static void main(String[] args) {

  String name = "Steve";
  int marks[] = new int[] { 89, 86, 94, 78, 91 };

  Student obj = new Student(name, marks);

  new WriteToFile().writeStudentObjectToFile(obj);
 }

 public void writeStudentObjectToFile(Student obj) {

  OutputStream os = null;
  ObjectOutputStream oos = null;
  
  try {
   os = new FileOutputStream("D://student.txt");
   oos = new ObjectOutputStream(os);
   oos.writeObject(obj);
   oos.flush();
   System.out.println("Object written successfully to file");
   
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  
  } catch (IOException e) {
   e.printStackTrace();
  
  } finally {
   try {
    if (oos != null) oos.close();
   } catch (Exception ex) {}
  }
 }
}

OUPTUT:


Object written successfully to file


In next program we will read same file and extract Student Object from the file and print the values. 


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

public class ReadFromFile {

 public static void main(String[] args) {

  new ReadFromFile().readStudentObjectFromFile();
 }

 public void readStudentObjectFromFile() {
  InputStream is = null;
  ObjectInputStream ois = null;
  
  try {
   is = new FileInputStream("D://student.txt");
   ois = new ObjectInputStream(is);
   
   Student obj = (Student) ois.readObject();
   
   System.out.println("Student Name : " + obj.getName());
   System.out.print("Marks        : ");
   for (int mark : obj.getMarks()) {
    System.out.print(mark+", ");
   }
  
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  } finally {
   try {
    if (ois != null) ois.close();
   } catch (Exception ex) {}
  }
 }
}

OUTPUT:


Student Name : Steve
Marks        : 89, 86, 94, 78, 91,