package com.qiwu.chatbotbackend.GameEnvironment; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectFileUtil { // obj 任意可以序列化的对象 fileAllName 文件全名 public static boolean writeObjectToFile(Object obj, String fileAllName) { File file =new File(fileAllName); FileOutputStream out; try { out = new FileOutputStream(file); ObjectOutputStream objOut=new ObjectOutputStream(out); objOut.writeObject(obj); objOut.flush(); objOut.close(); return true; } catch (IOException e) { e.printStackTrace(); return false; } } // fileAllName 文件全名 public static Object readObjectFromFile(String fileAllName) { Object temp=null; File file =new File(fileAllName); FileInputStream in; try { in = new FileInputStream(file); ObjectInputStream objIn=new ObjectInputStream(in); temp=objIn.readObject(); objIn.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return temp; } }