Serializable序列化(四)

package serializ;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * 我们知道有些类是单例
 * 如果序列化之后再反序列化,之前说过正常方式反序列化
 * 生成的对象肯定不一样,比如内存地址不一样
 * 那就打破了单例只有一个实例的设计与初衷
 * 如何保证在同一个应用里面反序列化之后仍然是同一个对象呢
 * 除了实现Serializable接口,还要实现一个方法
 * protected Object readResolve()
 * 
 * @author yli
 *
 */
public class SingletonSerializable {
	
	public static void main(String[] args) {
		Singleton s1 = Singleton.getInstance();
		System.out.println("内存地址===>" + s1);
		
		String file = "src/serializ/singleton.dat";
		writeObject(s1, file);
		
		Singleton s2 = (Singleton)readObject(file);
		
		System.out.println(s1 == s2);
		
	}
	

	// 序列化对象
	private static void writeObject(Object obj, String file) {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
			oos.writeObject(obj);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 反序列化
	private static Object readObject(String file) {
		ObjectInputStream ois = null;
		Object obj = null;
		try {
			ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
			obj = ois.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return obj;
	}

}

class Singleton implements Serializable {
	
	private static final long serialVersionUID = -7159883960112615379L;
	private final static Singleton onlyOne = new Singleton();
	
	/**
	 * 私有构造方法保证从外部无法创建新对象
	 * 不过java的反射机制可以利用setAccessable(true)可以调用到
	 * 这是后话了...
	 */
	private Singleton(){
		
	}
	
	/**
	 * 通过静态方法获得唯一的实例
	 * @return
	 */
	public static Singleton getInstance(){
		return onlyOne;
	}
	
	/**
	 * 要保证反序列化之后返回的还是唯一的实例对象
	 * 则要实现 readResolve方法
	 * 该方法会在反序列化之后被自动调用
	 * @return
	 */
	protected Object readResolve() {
		return getInstance();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
序列化是将对象转换为字节流的过程,以便可以将其存储在文件中、通过网络传输或在内存中进行持久化。在Java中,可以使用Serializable接口来实现对象的序列化和反序列化。 当一个实现了Serializable接口时,它表示该的对象可以被序列化序列化的过程是将对象转换为一系列字节,以便可以将其保存到磁盘文件或通过网络传输。反序列化则是将字节流重新转换为对象。 要使一个序列化,需要遵循以下几个步骤: 1. 实现Serializable接口。 2. 如果的实例变量引用了其他可序列化的对象,则这些对象也必须实现Serializable接口。 3. 为不希望被序列化的实例变量添加transient关键字。 以下是一个示例代码,演示了如何使用Serializable接口进行对象的序列化和反序列化: ```java import java.io.*; class MyClass implements Serializable { private int intValue; private String stringValue; public MyClass(int intValue, String stringValue) { this.intValue = intValue; this.stringValue = stringValue; } // 省略其他代码... public static void main(String[] args) { MyClass object = new MyClass(10, "Hello"); // 序列化对象 try { FileOutputStream fileOut = new FileOutputStream("object.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(object); out.close(); fileOut.close(); System.out.println("对象已序列化"); } catch (IOException e) { e.printStackTrace(); } // 反序列化对象 try { FileInputStream fileIn = new FileInputStream("object.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); MyClass deserializedObject = (MyClass) in.readObject(); in.close(); fileIn.close(); System.out.println("对象已反序列化"); System.out.println("intValue: " + deserializedObject.intValue); System.out.println("stringValue: " + deserializedObject.stringValue); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值