2008/12/11 12:47

serializable transient

transient : 직렬화(serializable)에서 제외 시킴

SerializableClass
package SerializableTest;
import java.io.Serializable;
public class SerializableClass implements Serializable {
 
 //선언부
 public String strTemp1 = "";
 public int intTemp1 = 0;
 public boolean bolTemp1 = false;
 public double dblTemp1 = 0F;
 
 public transient String strTemp2 = "대한민국";  //transient 기능 : 값을 전달하지 않음
 public transient int intTemp2 = 1;    //transient 기능 : 값을 전달하지 않음
 public transient boolean bolTemp2 = true;  //transient 기능 : 값을 전달하지 않음
 public transient double dblTemp2 = 1F;   //transient 기능 : 값을 전달하지 않음
 
 
 public String getStrTemp1() {
  return strTemp1;
 }
 public void setStrTemp1(String strTemp1) {
  this.strTemp1 = strTemp1;
 }
 public int getIntTemp1() {
  return intTemp1;
 }
 public void setIntTemp1(int intTemp1) {
  this.intTemp1 = intTemp1;
 }
 public boolean isBolTemp1() {
  return bolTemp1;
 }
 public void setBolTemp1(boolean bolTemp1) {
  this.bolTemp1 = bolTemp1;
 }
 public double getDblTemp1() {
  return dblTemp1;
 }
 public void setDblTemp1(double dblTemp1) {
  this.dblTemp1 = dblTemp1;
 }
 public String getStrTemp2() {
  return strTemp2;
 }
 public void setStrTemp2(String strTemp2) {
  this.strTemp2 = strTemp2;
 }
 public int getIntTemp2() {
  return intTemp2;
 }
 public void setIntTemp2(int intTemp2) {
  this.intTemp2 = intTemp2;
 }
 public boolean isBolTemp2() {
  return bolTemp2;
 }
 public void setBolTemp2(boolean bolTemp2) {
  this.bolTemp2 = bolTemp2;
 }
 public double getDblTemp2() {
  return dblTemp2;
 }
 public void setDblTemp2(double dblTemp2) {
  this.dblTemp2 = dblTemp2;
 }
 
 
 public SerializableClass(String s, int i, boolean b, double d) {
 
  //trensient에 값을 제외한 부분의 값 셋팅
 
  this.strTemp1 = s;
  this.intTemp1 = i;
  this.bolTemp1 = b;
  this.dblTemp1 = d;
 
 }
 
}


SerializableTest
package SerializableTest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializableTest {
 public static void main(String[] args) {
 
  try {
   // 파일에 객체를 직렬화
   SerializableClass s = new SerializableClass("직렬화가능", 100, true, 100F);
   FileOutputStream fos = new FileOutputStream("serial.txt");
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(s);
   oos.close();
     
   // 파일로 부터 객체를 해제 후 복원
   FileInputStream fis = new FileInputStream ("serial.txt");
   ObjectInputStream ois = new ObjectInputStream(fis);
   SerializableClass sc = (SerializableClass)ois.readObject();
   ois.close();
     
   System.out.println("== 직렬화 ====================");
   System.out.println("String  : " + sc.strTemp1);
   System.out.println("Integer : " + sc.intTemp1);
   System.out.println("Boolean : " + sc.bolTemp1);
   System.out.println("Double  : " + sc.dblTemp1);
 
   System.out.println("== 직렬화 않음   ==============");
    
   System.out.println("Transient String  : " + sc.strTemp2);
   System.out.println("Transient Integer : " + sc.intTemp2);
   System.out.println("Transient Boolean : " + sc.bolTemp2);
   System.out.println("Transient Double  : " + sc.dblTemp2);
   
   
    
  } catch(Exception e) {
   System.out.println("Exception : " + e);
  }
 }
}

 


결과값
== 직렬화 ====================
String  : 직렬화가능
Integer : 100
Boolean : true
Double  : 100.0
== 직렬화 않음(transient)   ==============
Transient String  : null
Transient Integer : 0
Transient Boolean : false
Transient Double  : 0.0
Trackback 0 Comment 0