프로그래밍 언어/JAVA(자바) 응용

66.직력화(serialization)

lroot 2022. 6. 6. 21:58
728x90
반응형

serialization이란?

- 인스턴스의 상태를 그대로 파일 저장하거나 네트워크로 전송하고(serialization) 이를 다시 복원(deserialization)하는 방식

- 자바에서는 보조 스트림을 활용하여 직렬화를 제공함

- ObjectInputStream과 ObjectOutputStream

생성자 설명
ObjectInputStream(InputStream In) InputStream을 생성자의 매개 변수로 받아 ObjectInputStream을 생성합니다.
ObjectOutputStream(OutputStream out) OutputStream을 생성자의 매개변수로 받아 ObjectOutputStream을 생성합니다.

 

Serializable 인터페이스

- 직렬화는 인스턴스의 내용이 외부로 유출되는 것이므로 프로그래머가 해당 객체에 대한 직력화 의도를 표시해야 함

- 구현 코드가 없는 marker interface

- transient : 직렬화 하지 않으려는 멤버 변수에 사용함 (Socket등 직렬화 할 수 없는 객체)

- SerializationTest.java

class Person implements Serializable{
String name;
String job;

public Person() {}

public Person(String name, String job) {
this.name = name;
this.job = job;
}

public String toString() {
return name + "," + job;
}
}

public class SerializationTest {

public static void main(String[] args) {

Person personLee = new Person("이순신","조선 대장군");
Person personKim = new Person("김유신","신라 대장군");

try(FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos)){

oos.writeObject(personLee);
oos.writeObject(personKim);
}catch(IOException e){

System.out.println(e);
}

try(FileInputStream fos = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fos)){

Person pLee = (Person)ois.readObject();
Person pKim = (Person)ois.readObject();

System.out.println(pLee);
System.out.println(pKim);

}catch(IOException e){
System.out.println(e);
}catch(ClassNotFoundException e) {
System.out.println(e);
}

}

}

 

Externalizable 인터페이스

- writerExternal()과 readExternal()메서드를 구현해야 함

- 프로그래머가 직접 객체를 읽고 쓰는 코드를 구현할 수 있음

 

- ExternalizationTest.java

class Person2 implements Externalizable{
String name;
String job;

public Person2() {}

public Person2(String name, String job) {
this.name = name;
this.job = job;
}

public String toString() {
return name + "," + job;
}

@Override
public void writeExternal(ObjectOutput obj) throws IOException {

obj.writeUTF(name);
obj.writeUTF(job);
}

@Override
public void readExternal(ObjectInput obj) throws IOException, ClassNotFoundException {

name = obj.readUTF();
job = obj.readUTF();
}
}

public class ExternalizationTest {

public static void main(String[] args) {

Person2 personLee = new Person2("이순신","조선 대장군");
Person2 personKim = new Person2("김유신","신라 대장군");

try(FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos)){

oos.writeObject(personLee);
oos.writeObject(personKim);
}catch(IOException e){

System.out.println(e);
}

try(FileInputStream fos = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fos)){

Person2 pLee = (Person2)ois.readObject();
Person2 pKim = (Person2)ois.readObject();

System.out.println(pLee);
System.out.println(pKim);

}catch(IOException e){
System.out.println(e);
}catch(ClassNotFoundException e) {
System.out.println(e);
}

}

}