![image]()
1 import java.io.*;
2
3 public class DemoClass4IO6 {
4 public static void main(String[] args) {
5 //TODO IO 文件复制 序列化 和 反序列化
6
7 //TOOD 数据文件对象
8 File dataFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io.dat");
9 //对象输出流
10 ObjectOutputStream oos = null;
11 //文件输出流
12 FileOutputStream fos = null;
13
14 //对象输入流
15 ObjectInputStream ois = null;
16 //文件输出流
17 FileInputStream fis = null;
18
19 try {
20
21 //TODO 写的过程,系统内存里的数据写入到系统外部文件里
22 //fos = new FileOutputStream(dataFile);
23 //oos = new ObjectOutputStream(fos);
24 //TODO: Java中只有增加了特殊的标记的类,才能再写文件中进行序列化操作
25 //TODO: 这里的标记其实就是一个接口, 这样:class User19 implements Serializable
26 /* User19 user19 = new User19();
27 oos.writeObject(user19);
28 oos.flush(); //产生:io.dat 文件*/
29
30 //TODO 写的过程,从文件中读取数据转换为对象
31 fis = new FileInputStream(dataFile);
32 ois = new ObjectInputStream(fis);
33 Object o = ois.readObject();
34 System.out.println(o); //结果:com.zhongyou.demopackage001.User19@1e643faf
35
36 }catch (Exception e){
37 throw new RuntimeException(e);
38 }finally {
39 if(oos != null){
40 try{
41 oos.close();
42 }catch (IOException e2){
43 throw new RuntimeException(e2);
44 }
45 }
46 }
47 }
48 }
49
50 class User19 implements Serializable {
51
52 }