java序列化工具類
阿新 • • 發佈:2019-02-11
class SerializationUtils { private SerializationUtils() { throw new UnsupportedOperationException(); } public static void writeObject(Serializable s, String fileNameAndPath) { ObjectOutputStream os = null; try { os = new ObjectOutputStream(new FileOutputStream(fileNameAndPath)); os.writeObject(s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static Object readObject(String fileNameAndPath) { ObjectInputStream oi = null; try { oi = new ObjectInputStream(new FileInputStream(fileNameAndPath)); return oi.readObject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (oi != null) { try { oi.close(); } catch (IOException e) { e.printStackTrace(); } } } throw new UnsupportedOperationException(); } }