package com.eadi.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializableUtil {
/**
*
* 将对象序列化到磁盘文件中
*
* @para Object
*
* @para File
*
* @return void
*
* @throwsException
*
*/
public static void writeObject(Object o, File file) throws Exception {
if (file.exists()) {
file.delete();
}
FileOutputStream os = new FileOutputStream(file);
// ObjectOutputStream 核心类
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(o);
oos.close();
os.close();
}
/**
*
* 反序列化,将磁盘文件转化为对象
*
* @para File
*
* @return Object
*
* @throwsException
*
*/
public static Object readObject(File f) throws Exception {
InputStream is = new FileInputStream(f);
// ObjectOutputStream 核心类
ObjectInputStream ois = new ObjectInputStream(is);
return ois.readObject();
}
public static void main(String[] args) throws Exception {
}
}
Java对象序列化和反序列化工具类
最新推荐文章于 2024-10-01 08:09:30 发布