JAVA IO流(对象的序列化)

来源:互联网 发布:数据仓储具有以下特征 编辑:程序博客网 时间:2024/05/16 10:59
package quickstart;import javax.print.DocFlavor;import java.io.*;/** * Created by patkritLee on 2017/3/23. */public class ObjectStreamDemo {    public static void main(String[] agrs)throws Exception{      // writeObj();       readObj();    }    public static void writeObj() throws IOException{        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));//将一个对象写到一个文件中去 现在在定义目的 字节        oos.writeObject(new Person("lisi",39,"KR"));//lisi和39都存在对象里,内存里 现在想让他们存储在硬盘上    }    public static void readObj() throws Exception{        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));        Person p = (Person)ois.readObject();        System.out.println(p);        ois.close();    }}class Person implements Serializable{    private String name;    transient int age;//不被序列化,不保存在硬盘里    static String country = "CN";//静态变量也不能被序列化    Person(String name, int age, String cty){        this.name = name;        this.age = age;        country =cty;    }    public String toString(){        return name+":"+age+":"+country;    }}

0 0
原创粉丝点击