티스토리 뷰

ㅇ 직렬화(Serialization) : 객체를 직렬화하여 전송 가능한 형태로 만드는 것.

    (객체들의 데이터를 연속적인 데이터로 변형하여 Stream을 통해 데이터를 읽도록 해주며 주로 객체들을

     통째로 파일로 저장,전송하고 싶을 때 주로 사용된다.)

 

ㅇ 역직렬화(Deserialization) : 직렬화된 파일 등을 역으로 직렬화하여 다시 객체의 형태로 만드는 것.

    (저장된 파일을 읽거나 전송된 Stream 데이터를 읽어 원래 객체의 형태로 복원한다.)

 

ㅇ 직렬화(스트림에 객체 출력) : ObjectOutputStream

    역직렬화(스트림으로부터 객체를 입력) : ObjectInputStream

 

ㅇ Object obj = new Object() => 직렬화 x

    Object obj = new Object("abc") => 직렬화 o

 

ㅇ public class Test implements Serializable{ ... }

    => Serializable 인터페이스를 구현해야 함(Test 클래스를 상속받는 클래스도 직렬화 가능함)

 

ㅇ 보통 클래스의 멤버변수 전부 직렬화 대상에 해당되나, 보안상의 문제나 기타 이유로 멤버변수의 일부를 제외하고

    싶다면 transient를 통해 지정할 수 있다.

   public class User implements Serializable {

private String id;

private transient String password;

private String email;

...

   }

 

ㅇ 다른 객체를 멤버변수로 사용하는 경우가 많은데 그 객체 클래스들 중 Serializable 인터페이스를 구현한 클래스가

    하나라도 없으면 직렬화할 수 없다.

   public class User implements Serializable {

private String id;

private transient String password;

private String email;

 

// Serializable 인터페이스를 구현한 클래스가 하나라도 없으면 Serialization 불가능

ItemInfo itemInfo;

Calendar calendar;

...

   }

 

ㅇ 직렬화된 객체를 역직렬화할 때는 직렬화 했을 때와 같은 클래스를 사용해야 한다. 클래스 이름이 같더라도 클래스의 내용이 번경될 경우 

    역직렬화 실패(클래스 버전이 같아야 함)

    (-> 객체가 직렬화될 때 클래스에 정의된 멤버들의 정보를 이용해서 serialVersionUID 라는 클래스의 버전을 자동생성해서 직렬화 내용에

         포함됨)

 

ㅇ ObjectInputStream, ObjectOutputStream을 주로 사용

   * User class

   public class User implements Serializable {

     // 추후에 User 클래스에 변경이 생길지라도 UID가 여전히 1이기 때문에 역직렬화를 성공적으로 할 수 있음(중요함)

private static final long serialVersionUID = 1L; 

 

private String id;

private transient String password;

private String email;

private int age;

 

public User(String id, String password, int age){

this.id = id;

this.password = password;

this.age = age;

}

 

public String toString() { // 오버라이드

return "("+id+", "+password+", "+age+")";

}

   }

 

* 직렬화 & 역직렬화

   public class Serialization{

private static final String USERINFO_SER = "user.user";

 

public static void main(String[] args){

conductSerializing();

conductDeserializing();

}

 

public static void conductSerializing(){

try{

FileOutputStream fos = new FileOutputStream(USERINFO_SER);

BufferedOutputStream bos = new BufferedOutputStream(fos);

ObjectOutputStream out = new ObjectOutputStream(bos);

 

User user1 = new User("홍길동", "1234", 30);

ArrayList list = new ArrayList();

list.add(user1);

 

out.writeObject(user1);

out.writeObject(list);

out.close();

} catch(Exception e){

e.printStackTrace();

}

  }

 

public static void conductDeserializing(){

try{

FileInputStream fis = new FileInputStream(USERINFO_SER);

BufferedInputStream bis = new BufferedInputStream(fis);

ObjectInputStream in = new ObjectInputStream(bis);

 

User user1 = (User) in.readObject();

ArrayList list = (ArrayList) in.readObject();

 

 

System.out.println(user1.toString());

System.out.println(list.toString());

in.close();

} catch(Exception e){

e.printStackTrace();

}

  }

  }

 

[출처] http://flowarc.tistory.com/entry/Java-객체-직렬화Serialization-와-역직렬화Deserialization

 

https://smartstore.naver.com/byrollin?

 

바이롤린 : 네이버쇼핑 스마트스토어

언제나 함께해요

smartstore.naver.com

 

'Java' 카테고리의 다른 글

Tree구조  (0) 2020.02.26
싱글톤 패턴  (0) 2016.04.26
클래스변수, 인스턴스 변수  (0) 2016.04.03
변수 초기화(생성자, Setter&Getter)  (0) 2016.04.03
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함