-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectInputStreamDemo.java
More file actions
45 lines (37 loc) · 973 Bytes
/
Copy pathObjectInputStreamDemo.java
File metadata and controls
45 lines (37 loc) · 973 Bytes
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
package io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
/**
* 对象字节输入流
*/
public class ObjectInputStreamDemo {
public static void main(String[] args) {
objectIn();
}
public static void objectIn() {
try {
InputStream is = new FileInputStream(
new File("d:" + File.separator + "test.txt"));
BufferedInputStream bis = new BufferedInputStream(is);
// 将缓冲字节输入流转换成对象字节输入流
ObjectInputStream ois = new ObjectInputStream(bis);
// 读取对象
Cat cat = (Cat) ois.readObject();
ois.close();
bis.close();
is.close();
System.out.println(cat.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}