-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathObjectReadWrite.java
More file actions
106 lines (99 loc) · 2.22 KB
/
Copy pathObjectReadWrite.java
File metadata and controls
106 lines (99 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Dept implements Serializable
{
int deptno = 100;
}
class Person // implements Serializable
{
Person(){
age = 1;
System.out.println("Person Class Cons Call");
}
int age;
}
class Employee extends Person implements Serializable
{
Dept dept = new Dept();
private static final long serialVersionUID = 1L;
/**
*
*/
private int id;
private String name;
private double salary;
private transient String password;
private double bonus;
private double pf;
Employee(){
password ="11111";
System.out.println("Employee Class Cons Call");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBonus() {
return bonus;
}
public void setBonus(double bonus) {
this.bonus = bonus;
}
public double getPf() {
return pf;
}
public void setPf(double pf) {
this.pf = pf;
}
}
public class ObjectReadWrite {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Employee ram = new Employee();
ram.age = 21;
ram.setId(1001);
ram.setName("ram");
ram.setSalary(9090);
FileOutputStream fo = new FileOutputStream("E:\\TestingFileHandling\\objectfile\\emp.dat");
ObjectOutputStream os = new ObjectOutputStream(fo);
os.writeObject(ram);
os.close();
fo.close();
System.out.println("Object Store....");
// Read
File file = new File("E:\\TestingFileHandling\\objectfile\\emp.dat");
if(file.exists()){
FileInputStream fi = new FileInputStream(file);
ObjectInputStream oi = new ObjectInputStream(fi);
Employee e = (Employee)oi.readObject();
System.out.println(e.getId()+" "+e.getName()+" "+e.getSalary()+" Password "+e.getPassword()+" Age "+e.age);
System.out.println(e.dept.deptno);
oi.close();
fi.close();
}
}
}