java-ObjectStream初步认识

import java.io.*;
class ObjectStreamDemo
{
<wbr>public static void main(String[] args) throws Exception, IOException, ClassNotFoundException<br><wbr>{<br><wbr><wbr>setObjectStream();<br><wbr><wbr>getObjSetStream();<br><wbr>}<br><wbr>public static void setObjectStream() throws IOException<br><wbr>{<br><wbr><wbr>ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>oos.writeObject(new Person("zhangsan", 20));<wbr> //对象的本地持久化存贮.将对象记录到文件,需要用ObjectInpuStream来读取.<br><wbr><wbr>oos.writeObject(new Person("lisi", 30));<br><wbr><wbr>oos.writeObject(new Person("wangwu", 25));</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>oos.close();<br><wbr>}</wbr></wbr></wbr>

<wbr>public static void getObjSetStream() throws Exception, IOException, ClassNotFoundException<br><wbr>{<br><wbr><wbr>ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));<wbr> //修改Person类后,读取的还是老文件,现在Person.class对应同一个UID值,现在也可以读取数据.</wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>Person p1 = (Person)ois.readObject();<br><wbr><wbr>Person p2 = (Person)ois.readObject();<br><wbr><wbr>Person p3 = (Person)ois.readObject();</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr>System.out.println(p1.toString());<br><wbr><wbr>System.out.println(p2.toString());<br><wbr><wbr>System.out.println(p3.toString());<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

class Person implements Serializable<wbr> //标记接口,类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。<br> {<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//对象进行了修改,则其对应的UID变化,记录到新文件(obj1.txt),如果读取的还是记录老版本对修改象的文件(obj.txt),就会抛出异常:<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//Exception in thread "main" java.io.InvalidClassException: Person; local class in<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>//compatible: stream classdesc serialVersionUID = 4491012183083119912, local class<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> //serialVersionUID = 4802475284912361980<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> //两次的类文件.class的serialVersionUID不一致,这时候,需要我们手动指定serialVersionUID的值.<br><wbr>public static final long serialVersionUID = 42L;<wbr> //自定义serialVersionUID,修改类时,读老版本文件可以读出.<br><wbr>//什么时候定义该UID号?如果类的修改不大,为了避免运行时抛出异常,就需要定义自己的UID号,使得不同版本的类文件指向同一个ID.</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>//不需要进行存贮的字段,需要用static、transient修饰.<br><wbr>private transient String name;<wbr> //transient - 瞬态<br><wbr>private int age;<br><wbr><wbr><wbr> private int num;<wbr><wbr> //修改的部分.<wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>Person(String name, int age)<br><wbr>{<br><wbr><wbr>this.name = name;<br><wbr><wbr>this.age = age;<br><wbr>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr>public String toString()<br><wbr>{<br><wbr><wbr>return "name=" + name + ", age=" + age;<br><wbr>}<br> }</wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(object)