对象序列化和反序列化工具类
package com.umu.util;
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.ObjectOutputStream;
import java.io.Serializable;
/**
* 对象序列化和反序列化工具类
* Created by Jim Calark on 2017/4/5.
*/
public class SerializationUtils {
private static String FILE_NAME= "D:/obj.bin";
//对象序列化
public static void writeObject(Serializable serializable){
try {
ObjectOutputStream
objectOutputStream = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
objectOutputStream.writeObject(serializable);
objectOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object readObject(){
Object object = null;
//反序列化
try {
ObjectInput objectInput
= new ObjectInputStream(new FileInputStream(FILE_NAME));
object = objectInput.readObject();
objectInput.close();
} catch
(IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException
e){
e.printStackTrace();
}
return object;
}
}