Java实现序列化接口-对象序列化

 

1.Serializable,实例化对象的过程:在内存中开辟存储空间并存储;
        1.序列化就是把对象—>转换成,二进制数据,便于对象的传输和保存;
        2.条件:必须实现一个标识接口:.io.Serializable;表示具有序列化功能;
        3.对象序列化常量:serialVersionUID:(final static long),定义当前序列化的版本ID,不同版本不同;
2.对象的输入、输出:
        1.ObjectOutputStream,ObjectInputStream,对象输出、对象输入流;
        2.对象输出流输出对象的过程,称序列化,被输出的对象称为序列化对象;对象输入流读入对象的过程,称反序列化,读入的是序列化对象,;
        3.序列化、反序列化,程序 与二进制数据的转换;
        4.序列化的内容:对象的属性:即保存对象的状态!
public interface Serializable;// 可序列化接口
Serializability of a class is enabled by the class implementing the .io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.
public class ObjectOutputStream
extends OutputStream
implements ObjectOutput, ObjectStreamConstants; // 对象字节输出流,对字节输出流的增强
example:
FileOutputStream fos = new FileOutputStream(“t.tmp”);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeInt(12345);
        oos.writeObject(“Today”);
        oos.writeObject(new Date());
        oos.close();
public ObjectOutputStream(OutputStream out)
                   throws IOException;  // 接收字节输出流;
public final void writeObject(Object obj)
                       throws IOException;// 写对象到输出流
public class ObjectInputStream
extends InputStream
implements ObjectInput, ObjectStreamConstants;// 对象字节输入流,对字节输入流的增强;
example:
   FileInputStream fis = new FileInputStream(“t.tmp”);
        ObjectInputStream ois = new ObjectInputStream(fis);
        int i = ois.readInt();
        String today = (String) ois.readObject();
        Date date = (Date) ois.readObject();
        ois.close();
public ObjectInputStream(InputStream in)
                  throws IOException;// 接收字节输入流,读入被序列化的对象状态信息
public final Object readObject()
                        throws IOException,
                               ClassNotFoundException;// 读取序列化对象 到输入流;
// 以上序列化是自动完成的;还可以自定义手工生成对象的序列化版本:
实现接口:
public interface Externalizable extends Serializable;        // 可以自定义如何保存对象的状态;
{
        void writeExternal(ObjectOutput out)
                   throws IOException;                        // 写入操作
        void readExternal(ObjectInput in)
                  throws IOException,
                         ClassNotFoundException; // 读入操作
}                                                                                    
public interface ObjectOutput extends DataOutput // 对象输出接口,大量的写操作可以实现;
{
         void         close()
          Closes the stream.
         void         flush()
          Flushes the stream.
        void         write(byte[] b)
          Writes an array of bytes.
        void         write(byte[] b, int off, int len)
          Writes a sub array of bytes.
        void         write(int b)
          Writes a byte.
         void         writeObject(Object obj)
          Write an object to the underlying storage or stream.
    // extends DataOutput:
    void write…(…)
                  throws IOException; // … Boolean, Byte, Byte[],Char, Char[], Double,Float, Int, Long,Short,UTF;
}
public interface ObjectInput extends DataInput // 对象输入接口,大量的读操作可以实现;
{
         int         available()
          Returns the number of bytes that can be read without blocking.
         void         close()
          Closes the input stream.
        int         read()
          Reads a byte of data.
        int         read(byte[] b)
          Reads into an array of bytes.
        int         read(byte[] b, int off, int len)
          Reads into an array of bytes.
        Object         readObject()
          Read and return an object.
         long         skip(long n)
          Skips n bytes of input.
     // extends DataInput:
     … read…()
                throws IOException;        // … Boolean, Byte, Byte[],Char, Char[], Double,Float, Int, Long,Short,UTF;
}
——-对象序列化操作:
package com.mldn;
import .io.File;
import .io.OutputStream;
import .io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
public class SerializableDemo
{
        public static void main(String[] args) throws IOException
        {
                String path = File.separator + “home” + File.separator + “administrator” + File.separator + “object.txt”;
                File file = new File(path);
                OutputStream out = null;                // 声明字节输出流
                ObjectOutputStream oos = null;        // 声明对象字节输出流
                out = new FileOutputStream(file);        // 实例化文件字节输出流
                oos = new ObjectOutputStream(out);        // 实例化对象字节输出流
                oos.writeObject(new Person(“王某”, 20));
                oos.close();
                out.close();               
        }
}
class Person implements java.io.Serializable
{
        private String name = null;
        private int age = 0;
        private static final long serialVersionUID = 1l;       
        public Person(String name, int age)
        {
                this.name = name;
                this.age = age;
        }
        public String toString()
        {
                return “姓名:” + this.name + ” 年龄:” + this.age;
        }
}
———反序列化操作:
package com.mldn;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
public class UnSerializable
{
        public static void main(String[] args) throws IOException, ClassNotFoundException
        {
                String path = File.separator + “home” + File.separator + “administrator” + File.separator + “object.txt”;
                File file = new File(path);
                InputStream in = null;                // 声明字节输入流
                ObjectInputStream ois = null;        // 声明对象字节输入流
                in = new FileInputStream(file);        // 实例化文件字节输入流
                ois = new ObjectInputStream(in);// 实例化对象字节输入流
                Object obj = ois.readObject();
                System.out.println(obj);               
                ois.close();
                in.close();               
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.UnSerializable
姓名:王某 年龄:20
*/
——————————————————————————————————————————————————————————————————————————————————————————————————————
———–实现自定义类序列化、反序列化行为:
package com.mldn;
import java.io.File;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectInput;
import java.io.IOException;
public class SeriDemo       
{
        public static void main(String[] args) throws IOException, ClassNotFoundException
        {
                seri();        // 测试序列化
                dseri();        // 测试反序列化
        }
        public static void seri() throws IOException // 对象序列化
        {
                String path = File.separator + “home” + File.separator + “administrator” + File.separator + “object.txt”;
                File file = new File(path);
                OutputStream out = null;                // 声明字节输出流
                ObjectOutputStream oos = null;        // 声明对象字节输出流
                out = new FileOutputStream(file);        // 实例化文件字节输出流
                oos = new ObjectOutputStream(out);        // 实例化对象字节输出流
                oos.writeObject(new Person(“王某”, 20));
                oos.close();       
                //out.write(1);        // 测试是否被关闭
        }
        public static void dseri() throws IOException, ClassNotFoundException// 反序列化
        {
                String path = File.separator + “home” + File.separator + “administrator” + File.separator + “object.txt”;
                File file = new File(path);
                InputStream in = null;                // 声明字节输入流
                ObjectInputStream ois = null;        // 声明对象字节输入流
                in = new FileInputStream(file);        // 实例化文件字节输入流
                ois = new ObjectInputStream(in);// 实例化对象字节输入流
                Object obj = ois.readObject();
                System.out.println(obj);               
                ois.close();
                //in.close();               
        }
}
// Person类用于测试序列化、反序列化操作
class Person implements java.io.Externalizable        // 实现手工序列化接口,自定义类对象序列化、反序列化时的行为!
{
        private String name = null;
        private int age = 0;
        private static final long serialVersionUID = 1l;       
        public Person()
        {
                // 实现手工序列化时必须实现类的无参构造,这是反序列化时要调用的,否则会出现找不到类
        }
        public Person(String name, int age)
        {
                this.name = name;
                this.age = age;
        }
        public String toString()
        {
                return “姓名:” + this.name + ” 年龄:” + this.age;
        }
        // 覆写写操作
        public void writeExternal(ObjectOutput out) throws IOException
        {
                out.writeObject(this.name);        // 保存姓名
                out.writeInt(this.age);                // 保存年龄
        }
        // 覆写读操作
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
        {
                this.name = (String)in.readObject();        // 读取姓名
                this.age = in.readInt();                                // 读取年龄
        }
}
/*
administrator@xu-desktop:~$ java com.mldn.SeriDemo
姓名:王某 年龄:20
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Java实现序列化接口-对象序列化
本文地址:https://www.zhiletu.com/archives-105.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微