-
Notifications
You must be signed in to change notification settings - Fork 122
/
Reverse.java
53 lines (49 loc) · 1.46 KB
/
Reverse.java
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 io.serialize;
import java.io.*;
import java.util.Arrays;
public class Reverse {
public static void main(String[] args) {
//序列化
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
try (ObjectOutputStream os = new ObjectOutputStream(buffer)) {
try {
os.writeInt(12345);
} catch (IOException e) {
e.printStackTrace();
}
try {
os.writeUTF("Hello");
} catch (IOException e) {
e.printStackTrace();
}
try {
os.writeObject(Double.valueOf(123.456));
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(Arrays.toString(buffer.toByteArray()));
ByteArrayInputStream bufferin = new ByteArrayInputStream(
buffer.toByteArray()
);
//反序列化,反序列化时,由JVM直接构造出Java对象,不调用构造方法,构造方法内部的代码,在反序列化时根本不可能执行
try {
try (ObjectInputStream in = new ObjectInputStream(bufferin)) {
int n = in.readInt();
String s = in.readUTF();
Double d = (Double) (in.readObject());
System.out.println(n);
System.out.println(s);
System.out.println(d);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}