forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectStreamDemo.java
More file actions
132 lines (113 loc) · 4.13 KB
/
ObjectStreamDemo.java
File metadata and controls
132 lines (113 loc) · 4.13 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
package onlyfun.caterpillar;
import java.io.*;
import java.util.*;
public class ObjectStreamDemo {
public static void main(String[] args) {
User[] users = {new User("cater", 101),
new User("justin", 102)};
// 寫入新檔
writeObjectsToFile(users, args[0]);
try {
// 讀取檔案資料
users = readObjectsFromFile(args[0]);
// 顯示讀回的物件
for(User user : users) {
System.out.printf("%s\t%d%n", user.getName(), user.getNumber());
}
System.out.println();
users = new User[2];
users[0] = new User("momor", 103);
users[1] = new User("becky", 104);
// 附加新物件至檔案
appendObjectsToFile(users, args[0]);
// 讀取檔案資料
users = readObjectsFromFile(args[0]);
// 顯示讀回的物件
for(User user : users) {
System.out.printf("%s\t%d%n", user.getName(), user.getNumber());
}
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("沒有指定檔名");
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
}
// 將指定的物件寫入至指定的檔案
public static void writeObjectsToFile(
Object[] objs, String filename) {
File file = new File(filename);
try {
ObjectOutputStream objOutputStream =
new ObjectOutputStream(
new FileOutputStream(file));
for(Object obj : objs) {
// 將物件寫入檔案
objOutputStream.writeObject(obj);
}
// 關閉串流
objOutputStream.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
// 將指定檔案中的物件資料讀回
public static User[] readObjectsFromFile(
String filename)
throws FileNotFoundException {
File file = new File(filename);
// 如果檔案不存在就丟出例外
if(!file.exists())
throw new FileNotFoundException();
// 使用List先儲存讀回的物件
List<User> list = new ArrayList<User>();
try {
FileInputStream fileInputStream =
new FileInputStream(file);
ObjectInputStream objInputStream =
new ObjectInputStream(fileInputStream);
while(fileInputStream.available() > 0) {
list.add((User) objInputStream.readObject());
}
objInputStream.close();
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
User[] users = new User[list.size()];
return list.toArray(users);
}
// 將物件附加至指定的檔案之後
public static void appendObjectsToFile(
Object[] objs, String filename)
throws FileNotFoundException {
File file = new File(filename);
// 如果檔案不存在則丟出例外
if(!file.exists())
throw new FileNotFoundException();
try {
// 附加模式
ObjectOutputStream objOutputStream =
new ObjectOutputStream(
new FileOutputStream(file, true)) {
// 如果要附加物件至檔案後
// 必須重新定義這個方法
protected void writeStreamHeader()
throws IOException {}
};
for(Object obj : objs) {
// 將物件寫入檔案
objOutputStream.writeObject(obj);
}
objOutputStream.close();
}
catch(IOException e) {
e.printStackTrace();
}
}
}