《java序列化与反序列化》

 /**
  * 把对象流化到本地
  * @param obj 需要流化的对象
  * @return true成功false失败
  */
 public static boolean objectToTxt(Object obj){
  ObjectOutputStream outputStream=null;
  try {
   outputStream=new ObjectOutputStream(new FileOutputStream(new File("E:/wo.txt")));
   outputStream.writeObject(obj);
   return true;
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   if(null!=outputStream){
    try {
     outputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  return false;
 }
 /**
  * 把本地的对象文件转成实体
  * @param filePath 文件的路径
  * @return 想要的实体的实例
  */
 public static Object txtToObject(String filePath){
  ObjectInputStream inputStream=null;
  File file=new File(filePath);
  if(!file.exists())
   return null;
  try {
   inputStream=new ObjectInputStream(new FileInputStream(file));
   return inputStream.readObject();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   if(null!=inputStream){
    try {
     inputStream.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  return null;
 }

你可能感兴趣的:(java,反序列化,序列化,文件操作,流化)