forked from slgobinath/Java-Helps-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
138 lines (125 loc) · 3.64 KB
/
Main.java
File metadata and controls
138 lines (125 loc) · 3.64 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Student stu = createStudent();
System.out.println("Name: " + stu.getName());
System.out.println("Age: " + stu.getAge());
// Convert the object to stream
byte[] stream = toStream(stu);
// Print the array
System.out.println(Arrays.toString(stream));
Student convertedStu = toStudent(stream);
System.out.println("Name: " + convertedStu.getName());
System.out.println("Age: " + convertedStu.getAge());
// Give any filename
save(stu, "/home/gobinath/student.xxx");
Student newStu = read("/home/gobinath/student.xxx");
System.out.println("Name: " + newStu.getName());
System.out.println("Age: " + newStu.getAge());
}
/**
* Create a sample Student object.
*
* @return a Student object.
*/
public static Student createStudent() {
// Create a Student object
Student stu = new Student();
stu.setName("Alice");
stu.setAge(24);
return stu;
}
/**
* Convert a Student object into stream of bytes.
*
* @param stu
* Student object.
* @return stream of bytes
*/
public static byte[] toStream(Student stu) {
// Reference for stream of bytes
byte[] stream = null;
// ObjectOutputStream is used to convert a Java object into OutputStream
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);) {
oos.writeObject(stu);
stream = baos.toByteArray();
} catch (IOException e) {
// Error in serialization
e.printStackTrace();
}
return stream;
}
/**
* Convert stream of bytes to Student.
*
* @param stream
* byte array
* @return Student object
*/
public static Student toStudent(byte[] stream) {
Student stu = null;
try (ByteArrayInputStream bais = new ByteArrayInputStream(stream);
ObjectInputStream ois = new ObjectInputStream(bais);) {
stu = (Student) ois.readObject();
} catch (IOException e) {
// Error in de-serialization
e.printStackTrace();
} catch (ClassNotFoundException e) {
// You are converting an invalid stream to Student
e.printStackTrace();
}
return stu;
}
/**
* Save a student into a file using Serialization.
*
* @param stu
* the Student to save.
* @param fileName
* the location to save.
*/
public static void save(Student stu, String fileName) {
try (FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(stu);
} catch (FileNotFoundException e) {
// Error in accessing the file
e.printStackTrace();
} catch (IOException e) {
// Error in converting the Student
e.printStackTrace();
}
}
/**
* Reading Student object from the given file.
*
* @param fileName
* location of the file.
* @return converted Student object.
*/
public static Student read(String fileName) {
Student stu = null;
try (FileInputStream fis = new FileInputStream(fileName); ObjectInputStream ois = new ObjectInputStream(fis);) {
stu = (Student) ois.readObject();
} catch (FileNotFoundException e) {
// Error in accessing the file
e.printStackTrace();
} catch (IOException e) {
// Error in converting the Student
e.printStackTrace();
} catch (ClassNotFoundException e) {
// You are converting an invalid stream to Student
e.printStackTrace();
}
return stu;
}
}