package com.qiqiao.test2; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; /** * 对象<=>文件转换类 * @author Administrator * */ public class ObjectFileConvert { public static void object2File(Object obj,File outFile){ ObjectOutputStream oos = null; FileOutputStream fos = null; try { if(!outFile.exists()){ outFile.createNewFile(); } fos = new FileOutputStream(outFile); oos = new ObjectOutputStream(fos); oos.writeObject(obj); }catch (Exception e) { e.printStackTrace(); }finally{ if (oos != null) { try { oos.close(); } catch (Exception e2) { e2.printStackTrace(); } } if(fos != null){ try { fos.close(); } catch (Exception e2) { e2.printStackTrace(); } } } } public static Object file2Object(File inFile){ FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(inFile); ois = new ObjectInputStream(fis); return ois.readObject(); } catch (Exception e) { // TODO: handle exception }finally{ if(ois!= null){ try { ois.close(); } catch (Exception e2) { e2.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } } return null; } }
时间: 2024-10-10 05:17:16