-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStream.java
More file actions
60 lines (49 loc) · 1.48 KB
/
Copy pathObjectStream.java
File metadata and controls
60 lines (49 loc) · 1.48 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
package io;
import java.io.*;
/**
* Created by helmeter on 5/10/16.
*/
public class ObjectStream {
/**
* @param args
*/
public static void main(String[] args) {
// TODO自动生成的方法存根
ObjectOutputStream objectwriter=null;
ObjectInputStream objectreader=null;
try {
objectwriter=new ObjectOutputStream(new FileOutputStream("img/student.txt"));
objectwriter.writeObject(new Student("gg", 22));
objectwriter.writeObject(new Student("tt", 18));
objectwriter.writeObject(new Student("rr", 17));
objectreader=new ObjectInputStream(new FileInputStream("img/student.txt"));
for (int i = 0; i < 6; i++) {
System.out.println(objectreader.readObject());
}
} catch (Exception e) {
// TODO自动生成的 catch 块
e.printStackTrace();
}finally{
try {
objectreader.close();
objectwriter.close();
} catch (IOException e) {
// TODO自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class Student implements Serializable{
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}