-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSerializeDemo.java
More file actions
77 lines (66 loc) · 1.74 KB
/
SerializeDemo.java
File metadata and controls
77 lines (66 loc) · 1.74 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
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Person
{
int age;
String city;
Person(){
System.out.println("Person Cons Call");
//age = 21;
//city="Delhi";
}
}
class Employee extends Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int id;
private String name;
private double salary;
private transient String pincode;
private double bonus=1111;
private double hra = 2222;
Employee(){
System.out.println("Employee this is the default cons");
id = 1001;
name="Ram";
salary = 9900;
}
Employee(int id , String name, double salary, String pincode){
System.out.println("Employee Param Cons");
this.id = id;
this.name = name;
this.salary = salary;
this.pincode = pincode;
super.age = 21;
super.city="Delhi";
}
void print(){
System.out.println("Id "+id);
System.out.println("Name "+name);
System.out.println("Salary "+salary);
System.out.println("Pincode is "+pincode);
System.out.println("Bonus "+bonus);
System.out.println("HRA "+hra);
System.out.println("Age "+super.age);
System.out.println("City "+super.city);
}
}
public class SerializeDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Employee employee = new Employee(1001,"Shyam",6666,"A111");
// Write Bytes into File
FileOutputStream fo =
new FileOutputStream("/Users/amit/Documents/testing/emp.dat");
ObjectOutputStream os = new ObjectOutputStream(fo);
os.writeObject(employee); // Write Object (// Convert Object into Bytes)
System.out.println("Object Store in a File");
os.close();
fo.close();
}
}