Object流

.object

 

 

 

 1 import java.io.FileInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.ObjectInputStream;
 6 import java.io.ObjectOutputStream;
 7 import java.io.Serializable;
 8 /**
 9  * 一.Object流
10  *        ObjectOutputStream流/ObjectInputStream流
11  * 二.补充Externalizable(可外部化的)接口 
12  * 作用:手动控制   序列可视化
13  * @Override
14                 public void writeExternal(ObjectOutput out) throws IOException {
15                 }
16                 
17                 @Override
18                 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
19                 }
20  * @author 罗摩衔那
21  *
22  */
23 public class TestObjectIO {
24     public static void main(String[] args) throws ClassNotFoundException {
25         T t=new T();
26         t.k=8;
27         try {//确定输出文件
28             FileOutputStream fos=new FileOutputStream("C:/Users/罗摩衔那/Desktop/Object.txt");
29             //给输出流套上ObjectOutputStream管道
30             ObjectOutputStream oos=new ObjectOutputStream(fos);
31             //将指定的对象写入ObjectOutputStream
32             oos.writeObject(t);
33             oos.flush();
34             oos.close();//关闭流
35             //从输出文件中读取信息
36             FileInputStream fis=new FileInputStream("C:/Users/罗摩衔那/Desktop/Object.txt");
37             //写入流上套上ObjectInputStream管道
38             ObjectInputStream ois=new ObjectInputStream(fis);
39             //从ObjectInputStream读取一个对象
40             T tReaded=(T) ois.readObject();
41             System.out.println(tReaded.i+" "+tReaded.j+" "+tReaded.d+" "+tReaded.k);
42         } catch (IOException e) {
43             
44             e.printStackTrace();
45         }
46     }
47 }
48 /*
49  * serializable(序列化)标记此类可序列化
50  */
51 class T implements Serializable
52 {
53     int i=0;
54     int j=9;
55     double d=2.3;
56     //透明
57     transient int k=0;
58 }

 

 

posted @ 2018-12-20 15:00  静心*尽力  阅读(246)  评论(0编辑  收藏  举报