-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectStreamTest.java
More file actions
66 lines (57 loc) · 1.58 KB
/
Copy pathObjectStreamTest.java
File metadata and controls
66 lines (57 loc) · 1.58 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
package java_base;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student1 implements Serializable{
private static final long serialVersionUID = 1L;
transient int id;
static int age;
private String name;
String dept;
public Student1(int id, String name, int age, String dept) {
// TODO Auto-generated constructor stub
this.id = id;
this.name = name;
this.age = age;
this.dept = dept;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Student1[id= " + id+ ", name = " + name + ", dept = " + dept + ", age = " + age + "]";
}
}
public class ObjectStreamTest {
private void savaObj() {
// TODO Auto-generated method stub
Student1 stu = new Student1(981036, "李明", 16, "CSD");
try {
FileOutputStream fos = new FileOutputStream("o.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(stu);
oos.close();
} catch (Exception e) {
// TODO: handle exception
System.err.println(e);
}
}
private void readObj()throws Exception {
// TODO Auto-generated method stub
Student1 stu;
FileInputStream fis = new FileInputStream("o.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
stu = (Student1)ois.readObject();
System.out.println(stu);
}
public static void main(String[] args)throws Exception{
// TODO Auto-generated method stub
ObjectStreamTest ost = new ObjectStreamTest();
ost.savaObj();
ost.readObj();
}
}
/*
* 存在read文件都会出现异常需要异常声明
*/