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

    对象序列化就是把一个对象变为二进制的数据流的一种方法

    通过对象序列化可以方便地实现对象的传输或存储

    如果一个类的对象想被序列化,则对象所在的类必须实现java.io.Serializable接口

    //类名:Person_3
    //属性:
    //方法:
    class Person_3 implements Serializable{		//此类的对象可以被序列化
    	private String name;
    	private int age;
    	
    	public Person_3(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	@Override
    	public String toString() {
    		return "姓名:" + name + ", 年龄:" + age;
    	}
    	
    	
    }
    

     此类的对象是可以经过二进制数据流进行传输的

    如果要完成对象的输入或者输出,还必须依靠对象输出流(ObjectOutputStream)对象输入流(ObjectInputStream).

    使用对象输出流输出序列化对象的步骤有时也称为序列化

    而使用对象输入流读入对象的过程有时也称为反序列化

    <1>对象输出流ObjectOutputStream

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    
    //=================================================
    // File Name       :	Serializable_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:Person_3
    //属性:
    //方法:
    class Person_3 implements Serializable{		//此类的对象可以被序列化
    	private String name;
    	private int age;
    	
    	public Person_3(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	@Override
    	public String toString() {
    		return "姓名:" + name + ", 年龄:" + age;
    	}
    	
    	
    }
    
    
    
    //主类
    //Function        : 	Serializable_demo;
    public class Serializable_demo {
    
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    		ObjectOutputStream oos = null;
    		OutputStream out = new FileOutputStream(f);		//文件输出流
    		oos = new ObjectOutputStream(out);						//为对象输出流实例化
    		oos.writeObject(new Person_3("张三", 30));
    		oos.close();
    	}
    
    }
    

    <2>对象输出流ObjectInputStream

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    
    //=================================================
    // File Name       :	Serializable_demo
    //------------------------------------------------------------------------------
    // Author          :	Common
    
    //类名:Person_3
    //属性:
    //方法:
    class Person_3 implements Serializable{		//此类的对象可以被序列化
    	private String name;
    	private int age;
    	
    	public Person_3(String name, int age) {
    		super();
    		this.name = name;
    		this.age = age;
    	}
    
    	@Override
    	public String toString() {
    		return "姓名:" + name + ", 年龄:" + age;
    	}
    	
    	
    }
    
    
    
    //主类
    //Function        : 	Serializable_demo;
    public class Serializable_demo {
    
    	public static void main(String[] args) throws Exception {
    		// TODO 自动生成的方法存根
    		
    		File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径
    		ObjectInputStream ois = null;
    		InputStream input = new FileInputStream(f);		//文件输入流
    		ois = new ObjectInputStream(input);						//为对象输入流实例化
    		Object obj = ois.readObject();									//读取对象
    		ois.close();
    		System.out.println(obj);
    	}
    
    }
    
  • 相关阅读:
    [考试反思]0904NOIP模拟测试37:守望
    游戏:最短路,拆点
    [考试反思]0903NOIP模拟测试36:复始
    [考试反思]0902NOIP模拟测试35:摆动
    长寿花:dp
    [考试反思]0901NOIP模拟测试34:游离
    赤壁情:dp
    [考试反思]0829NOIP模拟测试33:仰望
    [考试反思]0828NOIP模拟测试32:沉底
    宅男计划:单峰函数三分
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5281779.html
Copyright © 2011-2022 走看看