Java Program on Transient in Serialization
Free Java courses with 37 real-time projects - Learn Java
Program 1
package dataflair; import java.io.Serializable; public class Employee implements Serializable { private int eid; private String ename; private int esalary; transient private int mobile; public Employee(int eid, String ename, int esalary, int mobile) { this.eid = eid; this.ename = ename; this.esalary = esalary; this.mobile = mobile; } @Override public String toString() { return "Employee{" + "eid=" + eid + ", ename=" + ename + ", esalary=" + esalary + ", mobile=" + mobile + '}'; } }
Program 2
package dataflair; import java.io.*; public class TestRead { public static void main(String[] args) throws IOException,ClassNotFoundException { File F=new File("d://dataflair/emp.txt"); if(F.isFile()) { FileInputStream fis=null; ObjectInputStream ois=null; try { fis=new FileInputStream(F); ois=new ObjectInputStream(fis); Employee E1; E1=(Employee)(ois.readObject()); System.out.println(E1); } finally { } } else System.err.println("File not found........."); } }
Program 3
package dataflair; import java.io.*; public class TestWrite { public static void main(String[] args) throws IOException { File F=new File("d://dataflair/emp.txt"); FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos=new FileOutputStream(F); oos=new ObjectOutputStream(fos); Employee E=new Employee(101, "Ravi Gupta", 76000, 123456); fos.flush(); oos.writeObject(E); System.out.println("Record Save........."); } finally { fos.close(); oos.close(); } } }
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google