美文网首页Java
Java IO流(对象的序列化)

Java IO流(对象的序列化)

作者: 一亩三分甜 | 来源:发表于2019-10-03 11:44 被阅读0次

对象的序列化

import java.io.*;
public class Person11 implements Serializable
{
    private String name;
    int age;
    Person11(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    public String toString()
    {
        return name+":"+age;
    }
}
import java.io.*;

public class ObjectStreamDemo {
    public static void main(String[] args) throws Exception
    {
//        writeObj();
        readObj();
    }
    public static void readObj() throws Exception
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));

        Person11 p = (Person11)ois.readObject();

        System.out.println(p);

        ois.close();
    }

    public static void writeObj() throws IOException
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
        oos.writeObject(new Person11("lisi",39));
        oos.close();;
    }
}
//输出
lisi:39

将Person11中的内容改变,重新读取会报错。

import java.io.*;
public class Person11 implements Serializable
{
    String name;
    int age;
    Person11(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    public String toString()
    {
        return name+":"+age;
    }
}
import java.io.*;

public class ObjectStreamDemo {
    public static void main(String[] args) throws Exception
    {
//        writeObj();
        readObj();
    }
    public static void readObj() throws Exception
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));

        Person11 p = (Person11)ois.readObject();

        System.out.println(p);

        ois.close();
    }

    public static void writeObj() throws IOException
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
        oos.writeObject(new Person11("lisi",39));
        oos.close();;
    }
}
//输出
Exception in thread "main" java.io.InvalidClassException: Person11; local class incompatible: stream classdesc serialVersionUID = -4826887220003220322, local class serialVersionUID = 958454580975609894
    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:699)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1885)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1751)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2042)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1573)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:431)
    at ObjectStreamDemo.readObj(ObjectStreamDemo.java:13)
    at ObjectStreamDemo.main(ObjectStreamDemo.java:7)

静态成员不能被序列化

import java.io.*;
public class Person11 implements Serializable
{
    public static final long serialVersionUID = 42L;

    private String name;
    int age;
    static String country = "cn";
    Person11(String name,int age,String country)
    {
        this.name = name;
        this.age = age;
        this.country = country;
    }
    public String toString()
    {
        return name+":"+age+":"+country;
    }
}
import java.io.*;

public class ObjectStreamDemo {
    public static void main(String[] args) throws Exception
    {
//        writeObj();
        readObj();
    }
    public static void readObj() throws Exception
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));

        Person11 p = (Person11)ois.readObject();

        System.out.println(p);

        ois.close();
    }

    public static void writeObj() throws IOException
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
        oos.writeObject(new Person11("lisi0",399,"kr"));
        oos.close();;
    }
}
//输出
lisi0:399:cn

成员变量被transient修饰后,不能被序列化,保证其值在堆内存中存在,不在文本文件中存在。

import java.io.*;
public class Person11 implements Serializable
{
    public static final long serialVersionUID = 42L;

    private String name;
    transient int age;
    static String country = "cn";
    Person11(String name,int age,String country)
    {
        this.name = name;
        this.age = age;
        this.country = country;
    }
    public String toString()
    {
        return name+":"+age+":"+country;
    }
}
import java.io.*;

public class ObjectStreamDemo {
    public static void main(String[] args) throws Exception
    {
//        writeObj();
        readObj();
    }
    public static void readObj() throws Exception
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));

        Person11 p = (Person11)ois.readObject();

        System.out.println(p);

        ois.close();
    }

    public static void writeObj() throws IOException
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
        oos.writeObject(new Person11("lisi0",399,"kr"));
        oos.close();;
    }
}
//输出
lisi0:0:cn

序列化多个对象,每读取一个对象,结束符自动标记到这个对象的末尾。再读取时,是下一个对象。

import java.io.*;
public class Person11 implements Serializable
{
    public static final long serialVersionUID = 42L;

    private String name;
    int age;
    static String country = "cn";
    Person11(String name,int age,String country)
    {
        this.name = name;
        this.age = age;
        this.country = country;
    }
    public String toString()
    {
        return name+":"+age+":"+country;
    }
}
import java.io.*;

public class ObjectStreamDemo {
    public static void main(String[] args) throws Exception
    {
//        writeObj();
        readObj();
    }
    public static void readObj() throws Exception
    {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));

        Person11 p = (Person11)ois.readObject();

        System.out.println(p);

        Person11 p0 = (Person11)ois.readObject();

        System.out.println(p0);
        ois.close();
    }

    public static void writeObj() throws IOException
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
        oos.writeObject(new Person11("lisi0",399,"kr"));
        oos.writeObject(new Person11("lisi0",299,"Am"));
        oos.writeObject(new Person11("lisi0",199,"En"));
        oos.writeObject(new Person11("lisi0",99,"Ge"));
        oos.writeObject(new Person11("lisi0",9,"Fr"));
        oos.close();;
    }
}
//输出
lisi0:399:cn
lisi0:299:cn

相关文章

  • 序列化(java Serializeable、json、prot

    java序列化 序列化:将对象写入到IO流中反序列化:从IO流中恢复对象意义:序列化机制允许将实现序列化的Java...

  • Java序列化

    含义、意义使用场景 序列化:将对象写入到IO流中 反序列化:从IO流中恢复对象 意义:序列化机制允许将实现序列化的...

  • 每日一问(一) Serializable和Parcelable的

    1.序列化的定义 将对象写入到IO流中 2.反序列化的定义 从IO流中恢复对象 3.为什么要序列化? 永久性保存对...

  • 07-Java序列化面试题(10题)

    1、什么是java序列化,如何实现java序列化? 序列化就是一种用来处理对象流的机制,所谓对象流也就是将对象的内...

  • java用户登录注册

    用户登录注册 案例需求 涉及知识点 面向对象,接口,Java序列化,HashMap,IO流 欢迎界面 一级目录 登...

  • Java IO流(对象的序列化)

    对象的序列化 将Person11中的内容改变,重新读取会报错。 静态成员不能被序列化 成员变量被transient...

  • Java----IO操作

    Java----IO流操作 1、基础操作 2、补充要点 2.1、文件的拆分与合并 2.2、对象的序列化输出输入:将...

  • 2020-06-30【其它流】

    标准流 字节打印流 对象序列化流 Properties 特有方法 Properties和IO流结合的方法

  • 序列化和反序列化原理分析

    Java领域的对象如何传输 基于socket进行对象传输 当对象没有被序列化时,报异常(java.io.NotSe...

  • Day21--IO流

    对象的序列化 管道流 RandomAccessFile RandomAccessFile:该类不是算是IO体系中子...

网友评论

    本文标题:Java IO流(对象的序列化)

    本文链接:https://www.haomeiwen.com/subject/oajdpctx.html