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;
}
}
JAVA IO流(对象的序列化)
最新推荐文章于 2022-12-03 19:47:42 发布