package CHAPTER13;

import java.io.*;

class User1 implements Serializable {
    private static final long serialVersionUID = 1L;
    private String username;
    private transient String password; // 직렬화 제외 (transient)

    public User1(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String toString() {
        return "사용자명: " + username + ", 비밀번호: " + password;
    }
}

public class C13_11_SerializationDemo {
    public static void main(String[] args) {
        String filename = "user.ser"; // 직렬화된 객체 저장 파일

        // 1. 객체 직렬화 (저장)
        User1 user1 = new User1("Alice", "securePassword123");
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
            oos.writeObject(user1);
            System.out.println("객체 직렬화 완료: " + user1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 2. 객체 역직렬화 (읽기)
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
            User1 deserializedUser = (User1) ois.readObject();
            System.out.println("객체 역직렬화 완료: " + deserializedUser);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
