ABOUT ME

95.12.1 / 취준생 / Java

Today
Yesterday
Total
  • 16. serialization - (12) SerializationDemo ( 직렬화 )
    java/코드 리뷰 2020. 4. 27. 16:53
    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
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    package serialization;
     
    import java.io.Serializable;
     
    public class User implements Serializable {
        
        // 시리얼버전 아이디는 보통 1로 하는 편
        private static final long serialVersionUID = 1L;
        private int no;
        private String userId;
        private String password;
        private String tel;
        private String email;
        
        // transient : 취급하지 않음, 직렬화 대상에서 제외
        private transient UserGrade grade;
        
        public User () {
            
        }
     
        public User(int no, String userId, String password, String tel, String email) {
            super();
            this.no = no;
            this.userId = userId;
            this.password = password;
            this.tel = tel;
            this.email = email;
        }
     
        public int getNo() {
            return no;
        }
     
     
        public void setNo(int no) {
            this.no = no;
        }
     
     
        public String getUserId() {
            return userId;
        }
     
     
        public void setUserId(String userId) {
            this.userId = userId;
        }
     
     
        public String getPassword() {
            return password;
        }
     
     
        public void setPassword(String password) {
            this.password = password;
        }
     
     
        public String getTel() {
            return tel;
        }
     
     
        public void setTel(String tel) {
            this.tel = tel;
        }
     
     
        public String getEmail() {
            return email;
        }
     
        public void setEmail(String email) {
            this.email = email;
        }
        
        public UserGrade getGrade() {
            return grade;
        }
        
        public void setGrade(UserGrade grade) {
            this.grade = grade;
        }
        
    }
     
     
     
    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
    package serialization;
     
    public class UserGrade {
        
        private String grade;
        private int point;
        
        public UserGrade() {
            
        }
     
        public UserGrade(String grade, int point) {
            super();
            this.grade = grade;
            this.point = point;
        }
     
        public String getGrade() {
            return grade;
        }
     
        public void setGrade(String grade) {
            this.grade = grade;
        }
     
        public int getPoint() {
            return point;
        }
     
        public void setPoint(int point) {
            this.point = point;
        }
        
    }
     
     
    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
    package serialization;
     
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
     
    public class SerializationDemo {
        
        public static void main(String[] args) throws FileNotFoundException, IOException {
            
            User user = new User();
            user.setNo(100);
            user.setUserId("hong");
            user.setPassword("zxcv1234");
            user.setTel("010-1234-5678");
            user.setEmail("hong@gmail.com");
            
            UserGrade grade = new UserGrade();
            grade.setGrade("VIP");
            grade.setPoint(24000);
            
            FileOutputStream fos = new FileOutputStream("c:/files/user.sav");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            
            // user 참조변수가 참조하는 객체를 스트림으로 전송가능한 상태로 작성
            // 연결될 FileOutputStream으로 전송
            // FileOutputStream은 user.sav 파일로 출력됨
            oos.writeObject(user);
            
            oos.close();
        }
    }
     
     
    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
    package serialization;
     
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
     
    public class DeserializationDemo {
        
        public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
            
            FileInputStream fis = new FileInputStream("c:/files/user.sav");
            ObjectInputStream ois = new ObjectInputStream(fis);
            
            
            // 스트림으로 읽어온 직렬화된 객체의 정보를 바탕으로 User 객체 복원
            User user = (User) ois.readObject();
            
            System.out.println("번호 : " + user.getNo());
            System.out.println("아이디 : " + user.getUserId());
            System.out.println("비밀번호 : " + user.getPassword());
            System.out.println("전화번호 : " + user.getTel());
            System.out.println("이메일 : " + user.getEmail());
     
            
            ois.close();
        }
    }
     
     
Designed by Tistory.