集册 Java实例教程 通过流克隆对象

通过流克隆对象

欢马劈雪     最近更新时间:2020-01-02 10:19:05

478
通过流克隆对象
/* 
 来自 
*NowJava.com*/


//package com.nowjava;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;


public class Main {

    public static void main(String[] argv) throws Exception {

        Object obj = "nowjava.com";

        System.out.println(cloneObject(obj));

    }


    /**

     * Clone Object

     * @param obj

     * @return

     * @throws Exception

     */

    public static Object cloneObject(Object obj) throws Exception {/* 来自 nowjava.com - 时代Java*/


        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();


        ObjectOutputStream out = new ObjectOutputStream(byteOut);


        out.writeObject(obj);


        ByteArrayInputStream byteIn = 
展开阅读全文