프리비티브 타입
boolean , byte, short, int, long, float, double, char
무의미한 값들이 많은 희소배열을 직렬화할때는 의미있는 값만 직렬화를 하는 것이 경제적

선별적으로 직렬화 하기 위해서는 ?
프로그래머가 명령문을 직접 작성해야한다.

이전 코드에서는 writeObject 를 쓰면 객체가 알아서 직렬화가 되었다.

17-9 직접 직렬화 메소드를 작성
import java.io.*;
class DistrChart implements Serializable {
int arr[][];
DistrChart() {
arr = new int[10][10];
}
//private, void ,IOXception 으로 던지는 throw절
//private 으로 하는 이유?
private void writeObject(ObjectOutputStream out) throws IOException {
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[0].length; col++) {
if (arr[row][col] != 0) {
out.writeInt(row);//writeChar, writeDouble, writeObject
out.writeInt(col);
out.writeInt(arr[row][col]);
}
}
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
arr = new int[10][10];
try {
while (true) {
int row = in.readInt()
int col = in.readInt();
int data = in.readInt();
arr[row][col] = data; //호출되는 순서가 같아야한다.
}
}
catch (EOFException e) {
}
}
}
다른 클래스를 상속받는 직렬화 가능 클래스 (java.io.Serializable 인터페이스 상속)
17-12
class BookInfo extends GoodsInfo implements java.io.Serializable {
String writer; // 글쓴이
int page; // 페이지 수
BookInfo(String name, String code, int price, String writer, int page) {
super(name, code, price);
this.writer = writer;
this.page = page;
}
}
이 클래스의 객체를 직렬화 하면?
이 클래스의 객체를 역직렬화 하면?
자바의 역직렬화 메키니즘 때문.
객체가 역직렬화 될 때는 직렬화 가능 클래스 자체의 생성자는 호출 X
class BookInfo extends GoodsInfo implements Serializable
직렬화가 불가능한 가장 가까운 슈퍼클래스의 no-arg-constructor 자동 호출
GoodsInfo
해결


GoodsInfo,Object 클래스의 필드는 직렬화 X
17-15
import java.io.*;
class BookInfo extends GoodsInfo implements Serializable {
String writer; // 글쓴이
int page; // 페이지 수
BookInfo(String name, String code, int price, String writer, int page) {
super(name, code, price); //부모클래스의 생성자를 호출
this.writer = writer;
this.page = page;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.writeUTF(code);
out.writeUTF(name);
out.writeInt(price);
out.writeUTF(writer);
out.writeInt(page);
//out.defaultWriteObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
code = in.readUTF();
name = in.readUTF();
price = in.readInt();
writer = in.readUTF();
page = in.readInt();
//in.defaultReadObject();
}
}


이 스트림에서 현재 클래스의 비정적 필드 및 비과도적 필드를 읽습니다. 이는 역직렬화 중인 클래스의 readObject 메서드에서만 호출할 수 있습니다. 그렇지 않으면 Not Active Exception이 느려집니다.