import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class OffsetUtil {
private OffsetUtil() {
}
/**
* 判断文件存不存在
*
* @param path 文件路径
* @return 存在是true,否则为false
*/
public static boolean exOffsetFile(String path) {
File file = new File(path);
return file.exists();
}
/**
* 将map进行序列化
*
* @param hmap map对象
* @param path 生成序列化文件地址
* @throws PluginException 创建IO异常
*/
public static void mapSerialize(Map<Integer, Date> hmap, String path) throws PluginException {
try (FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fos)) {
oos.writeObject(hmap);
} catch (IOException e) {
throw new PluginException("map serialize error : " + e);
}
}
/**
* 将文件反序列化为map对象
*
* @param path 文件地址
* @return map 对象
*/
public static Map<Integer, Date> mapDeserialization(String path) throws PluginException {
HashMap<Integer, Date> map;
try (FileInputStream fis = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fis)) {
map = (HashMap<Integer, Date>) ois.readObject();
return map;
} catch (IOException | ClassNotFoundException e) {
throw new PluginException("map deserialization error : " + e);
}
}
}
Java Map对象的序列化和反序列化
最新推荐文章于 2024-05-31 10:27:51 发布