본문 바로가기
✨ Java/etc

자바(JAVA) - 직렬화(Serialization)

by 환풍 2023. 7. 24.
728x90

직렬화란?

객체 인스턴스의 데이터를 I/O스트림에 적합한 일련의 데이터로 변환하는 과정이다.

객체 인스턴스는 메모리 주소 값을 저장하고 있기 때문에, 메모리 주소 값을 그대로 송신하면 수신받는 쪽에서 아무 쓸모 없는 데이터가 된다. 따라서 객체 인스턴스의 값 형태로 변환이 필요하다.

 

MemberClass

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package io3;
import java.io.Serializable;
 
public class Member implements Serializable {
 
    private String id;
    private int age;
    private String address;
    
    public Member() {
        super();
    }
 
    public Member(String id, int age, String address) {
        super();
        this.id = id;
        this.age = age;
        this.address = address;
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
    @Override
    public String toString() {
        return "Member [id=" + id + ", age=" + age + ", address=" + address + "]";
    }
}
 
cs

클래스 객체에 직렬화를 가능하게 하려면, Serializable 이라는 인터페이스를 구현해야한다.

 

 

Main메서드

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package io3;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
public class IoEx3 {
    final static String PATH= "C:\\Users\\qkrwn\\javaWork\\zon\\src\\io3\\Member.txt";
 
    public void objectOutputEx() {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File(PATH)));
            oos.writeObject(new Member("Blue"23"부산시 수영구"));
            System.out.println("Serializable Accuess");
            
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(oos != null) oos.close();
            } catch (Exception e2) {
                
            }
        }
    }
    
    public void objectInputEx() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(new File(PATH)));
            System.out.println(ois.readObject());
        } catch (Exception e) {
            
        }finally {
            try {
                if(ois != null)ois.close();
            } catch (Exception e2) {
            }
        }
    }
    
    public static void main(String[] args) {
        IoEx3 i = new IoEx3();
        i.objectOutputEx();
        i.objectInputEx();
    }
}
cs

I/O Stream을 통해 직렬화 객체를 입출력해보았다.


  • transient : 직렬화 대상 제외

MemberClass

만약, 직렬화하려는 클래스에서 제외하고 싶은 필드가 있다면, transient를 붙여 직렬화 대상에서 제외할 수 있다.

제외된 값은 null(int는 0)로 처리된다.

 

반응형

댓글