반응형
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

bro's coding

java.Object Serialization(객체 직렬화)/Object DeSerialization(객체 역직렬화) 본문

[IT]/java

java.Object Serialization(객체 직렬화)/Object DeSerialization(객체 역직렬화)

givemebro 2021. 3. 15. 20:36
반응형

객체 직렬화(Object Sreialization)

메모리 상(heap 영역)에 있는 객체의 정보를 연속적인 데이터 상태로 변경하여
외부로 전송할 수 있는 상태로 만드는 것

객체 역직렬화(Object DeSerialization)

외부에 있는 정보를 객체로 복원하여 메모리에 적재하는 것

 

 

객체 직렬화 되어 메모리에 있는 정보가 외부로 전송되기 위해서 해당 클래스는 implements Serializable 해야함

외부로 직렬화되어 정보가 전송 되기 위한 객체의 클래스는
java.io.Serializable interface의 계층구조의 하위로 편입될 때만 가능

 

객체 직렬화를 위한 스트림

ObjectOutputStream class의 writeObject(object)

객체 역직렬화를 위한 스트림

ObjectInputStream class의 readObject():Object

 

package step6;

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

public class ObjectSerialService {
	private String path;

	public ObjectSerialService(String path) {
		this.path = path;
	}

	// FileOutputStream < ObjectOutputStream
	// writeObject(전송할 객체);
	// throws xxException
	// try finally 구조로 close를 해준다
	public void outputObj(Account account) throws FileNotFoundException, IOException {
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream(path));
			oos.writeObject(account);
		} finally {
			if (oos != null) {
				oos.close();
			}
		}

	}

	public Account inputObj() throws FileNotFoundException, IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		Account account = null;
		ObjectInputStream ois = null;

		try {
			ois = new ObjectInputStream(new FileInputStream(path));
			account = (Account) ois.readObject();
		} finally {
			if (ois != null) {
				ois.close();
			}
		}
		return account;
	}

}
package step6;

import java.io.FileNotFoundException;
import java.io.IOException;

public class TestObjectDeSerialization2 {
	public static void main(String[] args) {
		String path = "C:\\4. kosta215\\iotest5\\account.obj";
		ObjectSerialService service = new ObjectSerialService(path);
		Account account;
		try {
			account = service.inputObj();
			System.out.println(account.getName());
			System.out.println(account.getMoney());
			System.out.println(account.getPassword());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
package step6;

import java.io.FileNotFoundException;
import java.io.IOException;

public class TestObjectSerialization2 {
	public static void main(String[] args) {
		String path = "C:\\4. kosta215\\iotest5\\account.obj";
		ObjectSerialService service = new ObjectSerialService(path);

		try {
			service.outputObj(new Account("박보검", 100, "javaking"));
			System.out.println(path + "에 계좌객체 직렬화하여 전송");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
package step6;

import java.io.Serializable;

public class Account implements Serializable {

	private static final long serialVersionUID = -6327719185686084355L;
	private String name;
	private int money;
	// password 직렬화 제외
	// transient keyword : 직렬화 대상에서 제외
	private transient String password;

	public Account(String name, int money, String password) {
		super();
		this.name = name;
		this.money = money;
		this.password = password;
	}

	public int getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
 

java.SerialVersionUID

serialVersionUID 직렬화 대상 클래스(Serializable을 implements한 클래스)들은 개별 클래스마다 JVM에 의해 자신의 고유한 serialVersionUID를 가지게 된다. 이 때 클래스 정보( 인스턴스 변수)가 변경되면 다..

broscoding.tistory.com

 

반응형

'[IT] > java' 카테고리의 다른 글

java.Tread.sleep  (0) 2021.03.17
java.Thread  (0) 2021.03.17
java.FileMove(파일 이동)  (0) 2021.03.16
java.Object Serialization.transient(직렬화 제외)  (0) 2021.03.15
java.SerialVersionUID  (0) 2021.03.15
java.byte파일 컨트롤  (0) 2021.03.15
java.문자열 파일 컨트롤  (0) 2021.03.15
java.file입력/출력  (0) 2021.03.15
Comments