-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseStream.java
More file actions
59 lines (58 loc) · 1.34 KB
/
Copy pathUseStream.java
File metadata and controls
59 lines (58 loc) · 1.34 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
import java.io.*;
import java.util.*;
public class UseStream extends Object
{
public static void main(String[] args)
{
// 构建Vector对象
Vector v = new Vector();
v.add(0,"语文");
v.add(1,"数学");
v.add(2,"物理");
try
{
// 文件处理对象
File f = new File("temp.txt");
FileOutputStream fOut = new FileOutputStream(f);
ObjectOutputStream objOut = new ObjectOutputStream(fOut);
// 写入日期对象
objOut.writeObject(new Date());
// 写入Vector对象
objOut.writeObject(v);
objOut.close();
// 构建readObj的实例
readObj rObj = new readObj();
// 调用方法输出
rObj.readO();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
}
class readObj extends Object
{
public void readO()
{
try
{
File f = new File("temp.txt");
FileInputStream fIn = new FileInputStream(f);
ObjectInputStream objIn = new ObjectInputStream(fIn);
// 读取对象输出
Object ob1 = objIn.readObject();
System.out.println(ob1);
Object ob2 = objIn.readObject();
System.out.println(ob2);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}