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

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



  • 11
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
Java对象序列化反序列化是通过实现Serializable接口来完成的。下面是实现Java对象序列化反序列化的步骤: 1. 创建一个类并实现Serializable接口。例如: ```java import java.io.Serializable; public class MyClass implements Serializable { private int id; private String name; // 省略构造函数和其他方法 // Getter和Setter方法 } ``` 2. 在需要序列化的地方,使用ObjectOutputStream类将对象写入到文件或流中。例如: ```java MyClass obj = new MyClass(); obj.setId(1); obj.setName("Example"); try { FileOutputStream fileOut = new FileOutputStream("file.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(obj); out.close(); fileOut.close(); System.out.println("对象序列化并保存到文件中"); } catch (IOException e) { e.printStackTrace(); } ``` 3. 在需要反序列化的地方,使用ObjectInputStream类从文件或流中读取对象。例如: ```java MyClass obj = null; try { FileInputStream fileIn = new FileInputStream("file.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); obj = (MyClass) in.readObject(); in.close(); fileIn.close(); System.out.println("对象已从文件中反序列化"); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } // 使用反序列化后的对象 if (obj != null) { System.out.println("ID: " + obj.getId()); System.out.println("Name: " + obj.getName()); } ``` 以上就是实现Java对象序列化反序列化的基本步骤。需要注意的是,被序列化的类必须实现Serializable接口,且类的所有非瞬态(transient)成员变量也将被序列化

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值