๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java

[Java-๊ธฐ์ดˆ] ์ง๋ ฌํ™”

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 23.
๋ฐ˜์‘ํ˜•

โ–ถ ์ง๋ ฌํ™”

 

*์ง๋ ฌํ™”์™€ ์—ญ์ง๋ ฌํ™”

ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ์ธ์Šคํ„ด์Šค์˜ ์ƒํƒœ, ์ฆ‰ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ๊ฐ’์€ ๋งˆ์น˜ ์ƒ๋ช…์ฒด์ฒ˜๋Ÿผ ๊ณ„์† ๋ณ€ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ธ์Šคํ„ด์Šค์˜ ์–ด๋Š ์ˆœ๊ฐ„ ์ƒํƒœ๋ฅผ ๊ทธ๋Œ€๋กœ ์ €์žฅํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ์ „์†กํ•  ์ผ์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์ด๋ฅผ '์ง๋ ฌํ™”'๋ผ๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ €์žฅ๋œ ๋‚ด์šฉ์ด๋‚˜ ์ „์†ก๋ฐ›์€ ๋‚ด์šฉ์„ ๋‹ค์‹œ ๋ณต์›ํ•˜๋Š” ๊ฒƒ์„ '์—ญ์ง๋ ฌํ™”'๋ผ๊ณ  ํ•œ๋‹ค.

๋‹ค์‹œ ๋งํ•ด ์ง๋ ฌํ™”๋ž€ ์ธ์Šคํ„ด์Šค ๋‚ด์šฉ์„ ์—ฐ์† ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ์ง๋ ฌํ™” ๊ณผ์ •์—์„œ ํ•˜๋Š” ์ผ์€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜ ๊ฐ’์„ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค.

์ƒ์„ฑ์ž

์„ค๋ช…

ObjectInputStream(InputStream in)

InputStream์„ ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ObjectInputStream์„ ์ƒ์„ฑํ•œ๋‹ค.

ObjectOutputStream(OutputStream out)

OutputStream์„ ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ObjectOutputStream์„ ์ƒ์„ฑํ•œ๋‹ค.

โ€‹

โ€‹

SerializationTest.java

package stream.serialization;

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

class Person {
	private static final long serialVersionUID = -1503252402544036183L; // ๋ฒ„์ „ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•œ ์ •๋ณด
	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) throws ClassNotFoundException {
		Person personAhn = new Person("์•ˆ์žฌ์šฉ", "๋Œ€ํ‘œ์ด์‚ฌ");
		Person personKim = new Person("๊น€์ฒ ์ˆ˜", "์ƒ๋ฌด์ด์‚ฌ");

		try (FileOutputStream fos = new FileOutputStream("serial.out");
				ObjectOutputStream oos = new ObjectOutputStream(fos)) {
			oos.writeObject(personAhn); 
			oos.writeObject(personKim); // personAhn๊ณผ personKim์˜ ๊ฐ’์„ ํŒŒ์ผ์— ์”€(์ง๋ ฌํ™”)
		} catch (IOException e) {
			e.printStackTrace();
		}
		try (FileInputStream fis = new FileInputStream("serial.out");
				ObjectInputStream ois = new ObjectInputStream(fis)) {
			Person p1 = (Person) ois.readObject();
			Person p2 = (Person) ois.readObject(); // personAhn๊ณผ personKim์˜ ๊ฐ’์„ ํŒŒ์ผ์—์„œ ์ฝ์–ด๋“ค์ž„(์—ญ์ง๋ ฌํ™”)

			System.out.println(p1);
			System.out.println(p2);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

โ€‹

โ€‹

*Serializable ์ธํ„ฐํŽ˜์ด์Šค

์œ„ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋ฉด ์ถœ๋ ฅํ™”๋ฉด์ฒ˜๋Ÿผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ง๋ ฌํ™”๋Š” ์ธ์Šคํ„ด์Šค ๋‚ด์šฉ์ด ์™ธ๋ถ€๋กœ ์œ ์ถœ๋˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง๋ ฌํ™”๋ฅผ ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋„๋ฅผ ํ‘œ์‹œํ•ด์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Person ํด๋ž˜์Šค์— ๋งˆ์ปค ์ธํ„ฐํŽ˜์ด์Šค์ธ Serializable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€ํ•œ๋‹ค.

โ€‹<์‹คํ–‰ ๊ฒฐ๊ณผ>

์ฒ˜์Œ ์ƒ์„ฑํ•œ ํด๋ž˜์Šค ๋‚ด์šฉ์ด ๊ทธ๋Œ€๋กœ ์ฝํžˆ๊ณ  ๋ณต์›๋œ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค

โ€‹

โ€‹

โ€‹

*transient ์˜ˆ์•ฝ์–ด

์ง๋ ฌํ™” ๋Œ€์ƒ์ด ๋˜๋Š” ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๊ฐ€ ์ง๋ ฌํ™”๋˜๊ณ  ๋ณต์›๋œ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ง๋ ฌํ™” ๋  ์ˆ˜ ์—†๋Š” ํด๋ž˜์Šค(socket ํด๋ž˜์Šค๋Š” ์ง๋ ฌํ™”๋  ์ˆ˜ ์—†์Œ)๊ฐ€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋กœ ์žˆ๋‹ค๊ฑฐ๋‚˜ ์ง๋ ฌํ™”ํ•˜๊ณ ์‹ถ์ง€ ์•Š์€ ๋ณ€์ˆ˜๊ฐ€ ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค. ๊ทธ๋Ÿด ๋•Œ transient ์˜ˆ์•ฝ์–ด๋ฅผ ์“ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ•ด๋‹น ๋ณ€์ˆ˜๋Š” ์ง๋ ฌํ™”๋˜๊ณ  ๋ณต์›๋˜๋Š” ๊ณผ์ •์—์„œ ์ œ์™ธ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ์ฒด ์ž๋ฃŒํ˜•์ธ ๊ฒฝ์šฐ์— null ๊ฐ’์ด ๋œ๋‹ค. ์ˆจ๊ฒจ์•ผ๋˜๊ฑฐ๋‚˜ ์•”ํ˜ธํ™”ํ•ด์•ผ๋˜๋Š” ์ •๋ณด์— ์‚ฌ์šฉํ•œ๋‹ค.

job ๋‚ด์šฉ์ด ์ €์žฅ๋˜์ง€ ์•Š์•˜์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

โ€‹

โ€‹

โ€‹

*Externalizable ์ธํ„ฐํŽ˜์ด์Šค

์ง๋ ฌํ™”ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋˜ ๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค๋Š” Externalizable์ด๋‹ค. Serializable ์ธํ„ฐํŽ˜์ด์Šค๋Š” ์ž๋ฃŒ๋ฅผ ์ฝ๊ณ  ์“ฐ๋Š” ๋ฐ ํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š๋Š”๋‹ค. ํ•˜์ง€๋งŒ Externalizable ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผ ํ•  ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ์ฒด์˜ ์ง๋ ฌํ™”์™€ ์—ญ์ง๋ ฌํ™”๋ฅผ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ง์ ‘ ์„ธ๋ฐ€ํ•˜๊ฒŒ ์ œ์–ดํ•˜๊ณ ์ž ํ•  ๋•Œ Externalizable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.

โ€‹

โ€‹

Externalizable.java

package stream.serialization;

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;

class Dog implements Externalizable {
	String name;

	public Dog() {
	}

	@Override
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeUTF(name); // ์“ธ ๋•Œ
	}

	@Override
	public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
		name = in.readUTF(); // ์ฝ์„ ๋•Œ
	}

	public String toString() {
		return name;
	}
}

public class ExternalizableTest {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Dog myDog = new Dog();
		myDog.name = "๋ฉ๋ฉ์ด";

		try(FileOutputStream fos = new FileOutputStream("external.out");
				ObjectOutputStream oos = new ObjectOutputStream(fos)) {
			oos.writeObject(myDog);
		} catch (IOException e) {
			e.printStackTrace();
		}

		FileInputStream fis = new FileInputStream("external.out");
		ObjectInputStream ois = new ObjectInputStream(fis);

		Dog dog = (Dog)ois.readObject();
		System.out.println(dog);
	}
}

<์‹คํ–‰ ๊ฒฐ๊ณผ>

โ€‹

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€