POJO:
Serialization:
DeSerialization:
public class Employee implements Serializable{
private int empId;
private String empName;
transient private double salary;
//setter and getter methods
}
|
Employee employee;
employee=new Employee();
employee.setEmpId(1);
employee.setEmpName("name");
employee.setSalary(65000.00);
File file=null;
FileOutputStream fileoutputStream=null;
ObjectOutputStream objectOutputStream=null;
file=new File("emp2.txt");
fileoutputStream=new FileOutputStream(file);
objectOutputStream=new ObjectOutputStream(fileoutputStream);
objectOutputStream.writeObject(employee);
System.out.println("inserted.");
|
File file=null;
FileInputStream fileInputStream=null;
ObjectInputStream objectInputStream=null;
file=new File("emp2.txt");
fileInputStream=new FileInputStream(file);
objectInputStream=new ObjectInputStream(fileInputStream);
Object object=objectInputStream.readObject();
Employee e=(Employee) object;
System.out.println(e.getEmpId()+"\t"+e.getEmpName()+"\t"+e.getSalary());
|