기억의 DataBase

객체 직렬화 본문

Java

객체 직렬화

Zester; 2019. 1. 31. 16:14
  • 객체 직렬화 
객체를 byte의 흐름으로 만들어서 전송(어딘가에 쓰겠다)

객체 직렬화가 되려면, 특별한 인터페이스가 필요 or 기본형이거나
> Serializable(마크 인터페이스 - 메소드가 없는 인터페이스)

객체를 통으로 저장하고, 통으로 불러오는 것이 용이

private transient String pw;
// transient 직렬화 할 때  제외 >> 객체 전송할 때 제외함

public class User implements Serializable{
       
       //User가 가지는 필드들의 Type도 직렬화가 되어야 한다
       private String name;
       private String id;
       private transient String pw; // transient 직렬화에 제외 >> 전송할 때 제외함
       private int age;
       
       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 String getId() {
             return id;
       }
       public void setId(String id) {
             this.id = id;
       }
       public String getPw() {
             return pw;
       }
       public void setPw(String pw) {
             this.pw = pw;
       }
}

 


public class ObjectOutputTest {
       public static void main(String[] args) {
             
             ObjectOutputStream oos = null;
             
             try {
                    oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
                    List<User> list = new ArrayList<User>();
                    
                    User user= new User();
                    user.setName("홍");
                    user.setId("Ho");
                    user.setPw("1234");
                    user.setAge(10);
                    list.add(user);
                    
                    User user2= new User();
                    user2.setName("길");
                    user2.setId("n");
                    user2.setPw("5678");
                    user2.setAge(20);
                    list.add(user2);
                    
                    User user3= new User();
                    user3.setName("동");
                    user3.setId("g");
                    user3.setPw("90");
                    user3.setAge(30);
                    list.add(user3);
                    
                    oos.writeObject(list);
                    
             }catch(Exception ex) {
                    ex.printStackTrace();
             }finally {
                    try {
                           oos.close();
                    }catch(Exception ex) {
                           ex.printStackTrace();
                    }
             }
       }
}

public class ObjectInputTest {
       public static void main(String[] args) {
             
             ObjectInputStream ois = null;
             
             try {
                    
                    File f = new File("object2.dat");
                    if(!f.exists()) {
                           System.out.println("object.dat 파일이 없습니다");
                        // System.exit(0) > 웹프로그래밍에서는 절대 사용 금지 > WAS가 꺼질 수 있음
                           return; // 메인 함수 종료
                    }
                    
                    ois = new ObjectInputStream(new FileInputStream(f));
                    //Object obj  = ois.readObject();
                    List<User> list = (ArrayList)ois.readObject();
                    
                    for(int i =0; i<list.size(); i++) {
                    
                           User user = list.get(i);
                    
                           System.out.println(user.getName());
                           System.out.println(user.getId());
                           System.out.println(user.getPw());
                           System.out.println(user.getAge());
                           System.out.println("------------------");
                    
                    }
             }catch(Exception ex) {
                    ex.printStackTrace();
             }finally {
                    try {
                           ois.close();
                    }catch(Exception ex) {}
             }
       }
}

'Java' 카테고리의 다른 글

JDBC  (0) 2019.02.22
I/O(Input / Output)  (0) 2019.01.31
기본형, 참조형, 배열, Object, String  (0) 2019.01.18
인터페이스  (0) 2019.01.18
상속, 추상클래스  (0) 2019.01.18
Comments