本帖最后由 qq496099229 于 2015-6-10 13:15 编辑
- /*
-
- */
- import java.io.*;
- class ObjectStreamDemo
- {
- public static void main(String[] args) throws Exception
- {
- writeObj();
- readObj();
- }
- //
- public static void readObj() throws Exception
- {
- ObjectInputStream ois=new ObjectInputStream(
- new FileInputStream("F:\\TestTXT\\obj.txt")); //读取序列文件
- Person p=(Person)ois.readObject();
- System.out.println(p);
- ois.close();
- }
- public static void writeObj() throws IOException
- {
- ObjectOutputStream oos=new
- ObjectOutputStream(new FileOutputStream("F:\\TestTXT\\obj.txt"));
- oos.writeObject(new Person("fsy",22,"cc"));//序列化对象类必须实现Serializable,内存里面的东西,你也看不出来
-
- oos.close();
- }
- }
复制代码
- import java.io.*;
- class Person implements Serializable //没有方法的接口,是标记接口
- {
- static final long serialVersionUID=42L;
- private String name;
- transient int age; //transient保证在堆的变量不能被序列化
- static String country="cn"; //static 存储在方法堆里面,不能被序列化
- Person(String name,int age,String country)
- {
- this.name=name;
- this.age=age;
- this.country=country;
- }
- public String toString()
- {
- return name+":"+age+":"+country;
- }
- }
复制代码
运行结果竟然是fsy:0:cc
|
|