Problem - How you can read from a persisted object from binary file? Solution : java.io.ObjectInputStream reads persist object from a binary file opened with help of java.io.FileInputStream. Prerequisite:- Employee object is already stored in c:/sunilos/sunilos.ser Program :Step 1 - Coding Create a text file c:/sunilos/TestReadEmployee.java and copy below contents.import java.io.FileInputStream; import java.io.ObjectInputStream; /** * A program to read from persist data file implemented Serializable interface. */ public class TestReadEmployee { public static void main(String[] args) throws Exception { ObjectInputStream in = new ObjectInputStream(new FileInputStream("c:/sunilos/sunilos.ser")); Employee emp = (Employee)in.readObject(); System.out.println("ID: " +emp.getId()); System.out.println("First Name: " + emp.getFirstName()); System.out.println("Last Name: " +emp.getLastName()); System.out.println("Temp Value: "+emp.getTempValue()); } } Step 2 - Deployment
Step 3 - Testing
OutputID: 1 First Name: Sun Last Name: Rays Temp Value: null <<Previous | Next>> |