transient 關鍵字 和 利用serializable 持久化對象至硬盤

import java.util.Date;

public class GuestLoggingInfo implements java.io.Serializable  
{  
    private Date loggingDate = new Date();  
    private String uid;  
    private transient String pwd;  
     
    GuestLoggingInfo()  
    {  
        uid = "guest";  
        pwd = "guest";  
    }  
    public String toString()  
    {  
        return uid + pwd;
     }
 public String getUid() {
  return uid;
 }
 public void setUid(String uid) {
  this.uid = uid;
 }
 public String getPwd() {
  return pwd;
 }
 public void setPwd(String pwd) {
  this.pwd = pwd;
 }

 

 

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


public class Test5 {
 public static void main(String[] args) throws IOException {
  GuestLoggingInfo guest = new GuestLoggingInfo();
  FileOutputStream os = new FileOutputStream("fileName.dat");
  ObjectOutputStream oos = new ObjectOutputStream(os);
  oos.writeObject(guest);
 }
}

 

 

 

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


public class Test6 {
 public static void main(String[] args) throws IOException, ClassNotFoundException {
  FileInputStream fis = new FileInputStream("fileName.dat");
  ObjectInputStream ois = new ObjectInputStream(fis);
  GuestLoggingInfo guest = (GuestLoggingInfo)ois.readObject();
  System.out.println(guest.getPwd());
 }
}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章