zoukankan      html  css  js  c++  java
  • Java 对象序列化与反序列化

    对象序列化

    对象序列化

    对象序列化定义

    所谓的对象序列化就是将 保存在内存中的对象数据转换为二进制数据流进行传输的操作 ;但不是所有对象都可以进行序列化,要被序列化的的对象那么其所在的类一定要实现 java.io.Serializable 接口,该接口并没有认识的操作方法,因为该接口是一个 标识接口

    可以被序列化的类

    import java.io.Serializable;
    
    @SuppressWarnings("serial")
    class Book implements Serializable { 
    	private String title ; 
    	private double price ;
    	public Book(String title , double price) {
    		this.price = price ;
    		this.title = title ; 
    	}
    	public String toString() {
    		return "this.title + "	" + this.price";
    	}
    }
    

    如上,就实现了一个可以被序列化的类,(使用了压制警告)。

    如此,Book类就可以实现二进制的传输了!

    实现序列化和反序列化

    序列化类:

    • java.io.ObjectOutputStream
      • 将对象转为指定格式的二进制数据
    • 构造方法:
    public ObjectOutputStream(OutputStream out)
    
    • 输出对象:
    public final void writeObject(Object obj)
    

    反序列化类:

    • java.io.ObjectInputStream
      • 将已经序列化的对象转换回原本的对象内容
    • 构造方法:
    public ObjectInputStream(InputStream in)
    
    • 读取对象:
    public final Object readObject()
    

    实现序列化对象操作

    @SuppressWarnings("serial")
    class Book implements Serializable { 
    	private String title ; 
    	private double price ;
    	public Book(String title , double price) {
    		this.price = price ;
    		this.title = title ; 
    	}
    	public String toString() {
    		return this.title + "	" + this.price ;
    	}
    }
    public class TestDemo { 
    	public static void main(String [] args) throws Exception {
    		ser();
    	}
    	public static void ser() throws Exception {
    		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
    		oos.writeObject(new Book("Java开发",110.1));
    		oos.close();
    	}
    } 
    

    实现反序列化类

    	public static void dser() throws Exception {
    		ObjectInputStream ois = new ObjectInputStream(new FileInputStream (new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
    		Object obj = ois.readObject();// 按照Object读取
    		Book book = (Book) obj;// 向下转型
    		System.out.println(book);
    		ois.close();
    	}
    

    序列化与反序列化完整实现

    package helloworld;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    @SuppressWarnings("serial")
    class Book implements Serializable { 
    	private String title ; 
    	private double price ;
    	public Book(String title , double price) {
    		this.price = price ;
    		this.title = title ; 
    	}
    	public String toString() {
    		return this.title + "	" + this.price ;
    	}
    }
    public class TestDemo { 
    	public static void main(String [] args) throws Exception {
    		ser();  // 实现序列化
    		dser(); // 实现反序列化
    	}
    	public static void ser() throws Exception {
    		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
    		oos.writeObject(new Book("Java开发",110.1));// 输出
    		oos.close();
    	}
    	public static void dser() throws Exception {
    		ObjectInputStream ois = new ObjectInputStream(new FileInputStream (new File("F:" + File.separator + "demo" + File.separator + "Byte.txt")));
    		Object obj = ois.readObject();// 按照Object读取
    		Book book = (Book) obj;// 向下转型
    		System.out.println(book);
    		ois.close();
    	}
    } 
    

    transient 关键字

    通过序列化和反序列化的code实现,我们发现:序列化操作时是将整个对象的所有属性内容进行保存;但是如果某些属性的内容不需要被保存就可以通过 transient 关键字定义。

    private transient String title;
    

    由定义可知,title属性不可以被序列化操作。

    总结

    不是所有的类都需要被序列化,只有需要传输的对象所在的类才需要序列化对象。

  • 相关阅读:
    网络规划和布线
    网络编程
    NoSQL 非关系数据库
    OpenCV
    首页
    C++关键字
    TCP/IP 详解7 Ping指令
    TCP详解 (1)
    GRE封装解封装过程
    GRE tunnel
  • 原文地址:https://www.cnblogs.com/wangyuyang1016/p/11185561.html
Copyright © 2011-2022 走看看