ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 직렬화2-22.04.11
    고급자바 2022. 4. 11. 11:00
    728x90

    부모 클래스가 Serializable 인터페이스를 구현하고 있지 않을 경우
    부모객체의 필드값 처리 방법
        1. 부모 클래스가 Serializable 인터페이스를 구현하도록 해야 한다.
        2. 자식클래스에 writerObject()와 readObject()를 이용하여 부모객체의 필드값을 
             처리할 수 있도록 직접 구현한다.

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    
    public class T16_NonSerializableParentTest {
    	public static void main(String[] args) throws IOException, ClassNotFoundException {
    		FileOutputStream fos =
    				new FileOutputStream("d:D_Other/nonSerializableTest.bin");
    		ObjectOutputStream oos= new ObjectOutputStream(fos);
    		
    		Child child = new Child();
    		child.setParentName("부모");
    		child.setChildName("자식");
    		oos.writeObject(child);  //직렬화가 일어나면서 Object데이터가 fos에 저장이됨
    		oos.flush(); //생략가능
    		oos.close();
    		
    		///////////////////////////////////////////////
    		//읽는 작업 
    		ObjectInputStream ois=
    				new ObjectInputStream(
    				new FileInputStream("d:D_Other/nonSerializableTest.bin"));
    		
    		Child child2 =(Child) ois.readObject(); //역직렬화
    		System.out.println("parentName: "+child2.getParentName());
    		System.out.println("childName: "+child2.getChildName());
    		
    		ois.close();
    
    	}
    
    }

     

    //Serializable을 구현하지 않은 부모클래스
    class Parent{
    	private String parentName;
    
    	public String getParentName() {
    		return parentName;
    	}
    
    	public void setParentName(String parentName) {
    		this.parentName = parentName;
    	}
    	
    	
    }
    ////Serializable을 구현한 자식클래스
    class Child extends Parent implements Serializable{
    	
    	private String childName;
    
    	public String getChildName() {
    		return childName;
    	}
    
    	public void setChildName(String childName) {
    		this.childName = childName;
    	}
    	
    	
    	
    }

    =>부모는 직렬화 하지 않았기 때문이다. 


    class Parent implements Serializable  부모에 선언을 하면 나오는것을 확인할 수 있다.


    수정

    기본적인 자바 직렬화 또는 역직렬화 과정에서 별도의 처리가 필요할 때는 writeObject readObject 메서드를 클래스 내부에 선언해주면 된다. 물론 해당 클래스는 Serializable 인터페이스를 구현한 직렬화 대상 클래스여야 한다. 직렬화 과정에서는 writeObject가 역직렬화 과정에서는 readObject 메서드가 자동으로 호출된다.

    //Serializable을 구현한 자식클래스
    class Child extends Parent implements Serializable{
    	
    	private String childName;
    	public Child() {
    	      // TODO Auto-generated constructor stub
    	   }
    
    
    	public String getChildName() {
    		return childName;
    	}
    
    	public void setChildName(String childName) {
    		this.childName = childName;
    	}
    	
    	/**
    	 * 직렬화 될 때 자동으로 호출됨.
    	 * (접근제한자가 private이 아니면 자동 호출되지 않음.)
    	 */
    	private void writeObject(ObjectOutputStream out) throws IOException{
    		out.writeUTF(getParentName());  //부모객체 필드값 저장하기
    		out.defaultWriteObject(); 
    	}
    	
    	/**
    	 * 역직렬화가 될 때 자동으로 호출됨.
    	 * (접근제한자가 private이 아니면 자동 호출되지 않음.)
    	 */
    	private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException{
    		setParentName(in.readUTF()); //부모객체 필드값 읽어와 설정하기
    		in.defaultReadObject();
    	}
    	
    	
    	
    }

    728x90
Designed by Tistory.