ObjectInputStream, ObjectOutputStream 구현 대상 복제

자세히 보기
CloneUtil 클래스:
package com.bijian.study.clone;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class CloneUtil {

    public static void main(String[] args) {
        // 
        TemplateObject templateObject = new TemplateObject();
        templateObject.setAge(10);
        templateObject.setName("test");
        ArrayList descriptions = new ArrayList();
        descriptions.add(" ");
        descriptions.add(new String(" "));
        descriptions.add(new String(" "));
        descriptions.add(new String(" "));
        templateObject.setDescriptionList(descriptions);
        
        Style style = new Style();
        style.setLength(1600);
        style.setType(" ");
        Clothing clothing = new Clothing();
        clothing.setColor(" ");
        clothing.setSize(160);
        clothing.setStyle(style);
        templateObject.setClothing(clothing);
        
        System.out.println(templateObject);
        
        TemplateObject cloneTemplateObject = (TemplateObject) CloneUtil.getCloneObject(templateObject);
        
        System.out.println(cloneTemplateObject);
        
        System.out.println("templateObject==templateObject:" + (templateObject == cloneTemplateObject));
        System.out.println("templateObject.equals(templateObject):" + (templateObject.equals(cloneTemplateObject)));
        System.out.println("templateObject.toString().equals(templateObject.toString()):" + (templateObject.toString().equals(cloneTemplateObject.toString())));
    }
    
    //  
    public static Object getCloneObject(Object bean) {
        Object cloneBean = null;
        try {
            ByteArrayOutputStream byout = new ByteArrayOutputStream();
            ObjectOutputStream obj = new ObjectOutputStream(byout);
            obj.writeObject(bean);
            ByteArrayInputStream byin = new ByteArrayInputStream(byout.toByteArray());
            ObjectInputStream ins = new ObjectInputStream(byin);
            cloneBean = (Object) ins.readObject();
        } catch (Exception ex) {
            System.err.print(ex);
        }
        return cloneBean;
    }
}

TemplateObject 클래스:
package com.bijian.study.clone;

import java.io.Serializable;
import java.util.ArrayList;

public class TemplateObject implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 6790908190923407706L;
    
    private int age;
    private String name;
    private ArrayList descriptionList;
    private Clothing clothing;
    
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public ArrayList getDescriptionList() {
        return descriptionList;
    }
    public void setDescriptionList(ArrayList descriptionList) {
        this.descriptionList = descriptionList;
    }
    public Clothing getClothing() {
        return clothing;
    }
    public void setClothing(Clothing clothing) {
        this.clothing = clothing;
    }
    
    public String toString() {
        
        String baseInfo = String.valueOf(age) + name;
        
        String descriptions = "";
        for(int i=0;i 
  

      Clothing : 

package com.bijian.study.clone;

import java.io.Serializable;

public class Clothing implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -2876485615924815655L;
    private String color;
    private int size;
    private Style style;
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public Style getStyle() {
        return style;
    }
    public void setStyle(Style style) {
        this.style = style;
    }
    
    public String toString() {
        
        return color + String.valueOf(size) + style;
    }
}

스타일 클래스:
package com.bijian.study.clone;

import java.io.Serializable;

public class Style implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7052770518598516803L;
    private String type;
    private int length;
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public int getLength() {
        return length;
    }
    public void setLength(int length) {
        this.length = length;
    }
    
    public String toString() {
        
        return type + String.valueOf(length);
    }
}

실행 결과:
10test 160 1600
10test 160 1600
templateObject==templateObject:false
templateObject.equals(templateObject):false
templateObject.toString().equals(templateObject.toString()):true

좋은 웹페이지 즐겨찾기