1 import java.io.*
;
2
3 public class Test {
4
5 public static void main(String[] args) {
6
7 File f =
new File("a.txt"
);
8 try {
9 if(!
f.exists())
10 f.createNewFile();
11 }
catch (IOException e1) {
12 e1.printStackTrace();
13 }
14
15 try {
16 ObjectOutputStream oos =
new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream("a.txt"
)));
17 Employe em =
new Employe("xiaoming","123"
);
18 oos.writeObject(em);
19 oos.close();
20
21 ObjectInputStream ois =
new ObjectInputStream(
new BufferedInputStream(
new FileInputStream("a.txt"
)));
22 em =
(Employe)ois.readObject();
23 System.out.println(em.name);
24
25 }
catch (FileNotFoundException e) {
26 e.printStackTrace();
27 }
catch (IOException e) {
28 e.printStackTrace();
29 }
catch (ClassNotFoundException e) {
30 e.printStackTrace();
31 }
32 }
33 }
34
35 class Employe
implements Serializable {
//对象输出流的对象所对应的类应该实现Serializable接口
36 String name;
37 String id;
38
39 Employe(String name,String id){
40 this.name =
name;
41 this.id =
id;
42 }
43 }
FileOutputStream();还不太明白
转载于:https://www.cnblogs.com/Arvin-9/p/3913646.html
转载请注明原文地址: https://win8.8miu.com/read-1495231.html