如何复制一个java对象

如何复制一个java对象,第1张

/
      复制对象
     
      @param srcObj
      @return
     /
    public static Object depthClone(Object srcObj) {
        if (srcObj == null) {
            return null;
        }
        Object cloneObj = null;
        ByteArrayOutputStream out = null;
        ObjectOutputStream oo = null;
        ByteArrayInputStream in = null;
        ObjectInputStream oi = null;
        try {
            out = new ByteArrayOutputStream();
            oo = new ObjectOutputStream(out);
            oowriteObject(srcObj);
            in = new ByteArrayInputStream(outtoByteArray());
            oi = new ObjectInputStream(in);
            cloneObj = oireadObject();
        } catch (IOException e) {
            eprintStackTrace();
        } catch (ClassNotFoundException e) {
            eprintStackTrace();
        } finally {
            if (out != null) {
                try {
                    outclose();
                } catch (IOException ex) {
                    exprintStackTrace();
                }
            }
            if (oo != null) {
                try {
                    ooclose();
                } catch (IOException ex) {
                    exprintStackTrace();
                }
            }
            if (in != null) {
                try {
                    inclose();
                } catch (IOException ex) {
                    exprintStackTrace();
                }
            }
            if (oi != null) {
                try {
                    oiclose();
                } catch (IOException ex) {
                    exprintStackTrace();
                }
            }
        }
        return cloneObj;
    }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/12709932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-27
下一篇 2023-05-27

发表评论

登录后才能评论

评论列表(0条)

保存