• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Java对对象的序列化和反序列化

JAVA相关 水墨上仙 3097次浏览

Java提供了将对象以字节的形式写入到磁盘中,在需要的时候在从磁盘中恢复的功能,在网络传输中非常有用,下面将代码贴上,分别用两种形式实现了对象的序列化,其中第二种当时能将指定的变量序列化,更加人性化的随心所欲

package com.bird.thinking;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
 * @use 实现对对象的序列化和反序列化
 * @author Bird
 *
 */
public class Data {
	public static void writeObject() throws Exception{
		Worm worm = new Worm("Bird");
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d://worm.out"));
		out.writeObject(worm);
		out.close();//关闭的同时也刷新清空了缓冲区
	}
	
	
	public static void readObject() throws Exception{
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://worm.out"));
		Worm s2 = (Worm) in.readObject();
		System.out.println(s2);
	}
	
	public static void main(String[] args) throws Exception{
	//	writeObject();
		readObject();
	}
}
class Worm implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private String name = null;
	public Worm(String name){
		this.name = name;
	}
	
	public String toString(){
		return name;
	}
}

下面这是第二种方式

package com.bird.thinking;
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
/**
 * @use 第二种可控制序列化变量个数的方式
 * @author Bird
 *
 */
public class Blip3 implements Externalizable {
	private int i;
	private String s;//没有实例化
	public Blip3(){
		System.out.println("Blip3 Constructor!!");
	}
	//注意  这里的s没有实例化
	
	public Blip3(String x, int a){
		System.out.println("Blip3(String x, int a)");
		s = x;
		i = a;
		//s  和 i实例化在非默认构造函数中
	}
	
	public String toString(){
		return s + i;
	}
	
	public void writeExternal(ObjectOutput out){//可选择写入变量
		System.out.println("Blip3.writeExternal");
		try {
			out.writeObject(s);
			out.writeInt(i);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}//必须进行这两项,否则就一个变量也不出初始化
	}
	
	public void readExternal(ObjectInput in){//可选择读入数据
		System.out.println("Blip3.readExternal");
		try {
			s = (String)in.readObject();
			i = in.readInt();
		} catch (ClassNotFoundException e) {
			
		} catch (IOException e) {
		throw new RuntimeException(e);
		}
		
	}
	
	public void read() throws FileNotFoundException, IOException, ClassNotFoundException{//读取序列化的类
		ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://Blip3.out"));
		System.out.println("Revovering  b3");
		Blip3 b3 = (Blip3)in.readObject();
		System.out.println(b3);
	}
	
	public void write() throws Exception{//写入对象
		Blip3 b3 = new Blip3("A String", 47);
		System.out.println(b3);
		ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("d://Blip3.out"));
		System.out.println("Saving Object");
		o.writeObject(b3);
		o.close();
	}
	
	public static void main(String[] args) throws Exception{
		Blip3 b = new Blip3();
		//	b.write();
		b.read();
	}
}

输出结果

Blip3 Constructor!!
Blip3(String x, int a)
A String47
Saving Object
Blip3.writeExternal

输出结果

Blip3 Constructor!!
Revovering  b3
Blip3 Constructor!!
Blip3.readExternal
A String47


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明Java对对象的序列化和反序列化
喜欢 (0)
加载中……