java序列化工具类

来源:互联网 发布:网络教学的优点 编辑:程序博客网 时间:2024/04/30 01:06
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();}}

0 0
原创粉丝点击