- import java.io.*;
- public class ObjectStreamDemo {
- public static void main(String[] args)throws Exception
- {
- writeObject();
- readObject();
- }
- public static void readObject() throws Exception
- {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c:/Person.obj"));
- Person p1=(Person)ois.readObject();
- Person p2=(Person)ois.readObject();
- s.p(p1);
- s.p(p2);
- ois.close();
-
- }
- public static void writeObject() throws Exception
- {
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("c:/Person.obj"));
- oos.writeObject(new Person("lisi",20,"cn"));
- oos.writeObject(new Person("wangwu",30,"kr"));
- oos.close();
-
- }
- }
- class Person implements Serializable//记得要实现一下接口
- {
- static final long serialVersionUID = 43L;
- private String name;
- private transient int age;//非常像是static final的感觉 无法修改,无法序列
- private static String country = "usa";//没有被序列化
- @SuppressWarnings("static-access")
- Person(String name,int age,String country)
- {
- this.name=name;
- this.age=age;
- this.country=country;
- }
- public String toString()
- {
- return name+"::"+age+country;
-
- }
- }
复制代码 transient 这个关键字修饰后的变量,不可以被序列化 很像static
使用的过程中发现无法被修改,又非常像final修饰
大神能不能告诉我下 transient == static fianl 变成全局常量
底层是不是这样呀
疑问是transient 有没有修改的方法
|
|