-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializationTest.java
More file actions
53 lines (43 loc) · 1.18 KB
/
Copy pathSerializationTest.java
File metadata and controls
53 lines (43 loc) · 1.18 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
package com.intellect.io;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Address implements Serializable {
String addressLine, city, state;
public Address(String addressLine, String city, String state) {
this.addressLine = addressLine;
this.city = city;
this.state = state;
}
}
class Student implements Serializable {
int id;
String name;
Address address;
public Student(int id, String name, Address address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
}
public class SerializationTest {
public static void main(String[] args) {
try {
// Creating the object
Address a1 = new Address("abc colony", "kurnool", "AP");
Student s1 = new Student(101, "karthik", a1);
// Creating stream and writing the object
FileOutputStream fout = new FileOutputStream("D:/javaFile.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(s1);
// out.flush();
// closing the stream
fout.close();
out.close();
System.out.println("success");
} catch (Exception e) {
System.out.println(e);
}
}
}