熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java使用序列化實現深克隆

2022-06-13   來源: Java核心技術 

  序列化實現深克隆

  public static Object deepClone(Object source) {

  ObjectOutputStream oos = null;

  ObjectInputStream ois = null;

  File file = null;

  try {

  FileOutputStream fos = new FileOutputStream(objFile

  oos = new ObjectOutputStream(fos)

  ooswriteObject(source)

  FileInputStream fis = new FileInputStream(objFile

  ois = new ObjectInputStream(fis)

  return oisreadObject()

  } catch (Exception e) {

  Systemerrprintln(對象克隆失敗

  eprintStackTrace()

  return null;

  } finally {

  try {

  if(null != oos) {

  oosclose()

  }

  if(null != ois) {

  oisclose()

  }

  file = new File(objFile

  if(null != file) {

  filedelete()

  }

  } catch (IOException e) {

  eprintStackTrace()

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26113.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.