오답노트

[Java] 직렬화 본문

Java

[Java] 직렬화

권멋져 2023. 7. 11. 09:51

직렬화

인스턴스의 상태를 그대로 파일 저장하거나 네트웍으로 전송하고 이를 다시 복원하는 방식이다.

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

 

스트림을 활용한 직렬화

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Person implements Serializable{
	String name;
	transient String job; // 직렬화 안됨
	
	public Person() {}
	
	public Person(String name, String job) {
		this.name = name;
		this.job = job;
	}
	
	public String toString() {
		return name + "," + job;
	}
}

public class SeralizationTest {

	public static void main(String[] args) {
		Person personLee = new Person("이순신","대표이사");
		Person personKim = new Person("김유신","상무이사");
		
		try(FileOutputStream fos = new FileOutputStream("serial.txt");
				ObjectOutputStream ois = new ObjectOutputStream(fos)){
			ois.writeObject(personLee);
			ois.writeObject(personKim);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			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(pLee);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		} catch(ClassNotFoundException e) {
			System.out.println(e);
		}
		
	}

}

Externalizable

사용자가 직접 객체를 읽고 쓰는 코드를 구현할 수 있다.

인터페이스를 implements하여 가상함수를 구현해 사용한다.

 

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Person implements Externalizable{
	String name;
	String job; // 직렬화 안됨
	
	public Person() {}
	
	public Person(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 SeralizationTest {

	public static void main(String[] args) {
		Person personLee = new Person("이순신","대표이사");
		Person personKim = new Person("김유신","상무이사");
		
		try(FileOutputStream fos = new FileOutputStream("serial.txt");
				ObjectOutputStream ois = new ObjectOutputStream(fos)){
			ois.writeObject(personLee);
			ois.writeObject(personKim);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			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(pLee);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e);
		} catch(ClassNotFoundException e) {
			System.out.println(e);
		}
		
	}

}

'Java' 카테고리의 다른 글

[Java] Multi Thread Synchronization  (0) 2023.07.11
[Java] Thread  (0) 2023.07.11
[Java] IOStream  (0) 2023.07.10
[Java] 사용자 정의 예외 처리  (0) 2023.07.10
[Java] 예외 처리  (0) 2023.07.10