天天看點

對象深度克隆

使用輸入、輸出流:

public  static <R,T> T clone(R resouse,T dest){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
			oos = new ObjectOutputStream(baos);
			oos.writeObject(resouse);
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
			ois = new ObjectInputStream(bais);
            dest = (T) ois.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
			    if(null!=oos) {
                    oos.close();
                }
                if(null!=ois){
			        ois.close();
                }
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return dest;
	}
           

也可以使用json把對象轉成字元串,然後再反轉為對象:

https://mp.csdn.net/postedit/88535528