▶ 직렬화 대상에서 제외 transient
직렬화의 대상에서 제외시키고픈 인스턴스 변수가 잇다면 이를 transient로 선언하면 된다.
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 PersonalInfo implements Serializable {
String name;
transient String secretInfo;
int age;
transient int secretNum;
public PersonalInfo(String name, String sInfo, int age, int sNum) {
this.name = name;
secretInfo = sInfo;
this.age = age;
secretNum = sNum;
}
public void showCirlceInfo() {
System.out.println("name : " + name);
System.out.println("secret info : " + secretInfo);
System.out.println("age : " + age);
System.out.println("secret num : " + secretNum);
System.out.println("");
}
}
public class TransientMembers {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
// TODO Auto-generated method stub
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Personal.ser"));
PersonalInfo info = new PersonalInfo("John", "baby", 3, 42);
info.showCirlceInfo();
out.writeObject(info);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream("Personal.ser"));
PersonalInfo recovInfo = (PersonalInfo)in.readObject();
in.close();
recovInfo.showCirlceInfo();
}
}
[결과]
name : John
secret info : baby
age : 3
secret num : 42
name : John
secret info : null
age : 3
secret num : 0
[Java]File 클래스 (0) | 2012.10.01 |
---|---|
[Java]Random Aceess (0) | 2012.09.30 |
[Java]ObjectInputStream & ObjectOutputStream (0) | 2012.09.29 |
[Java]BufferedReader & BufferedWriter (0) | 2012.09.29 |
[Java]FileReader & FileWriter (0) | 2012.09.28 |
댓글 영역