您的位置:首页 > 编程语言 > Java开发

JAVA IO流(对象的序列化)

2017-03-23 14:47 204 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐