posts - 241,  comments - 116,  trackbacks - 0

transient 关键字表示在Serializable 的时候不保存该值。

//LoggingInfo.java

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;


public class LoggingInfo implements java.io.Serializable {

    private static final long serialVersionUID = 1L;
    private Date loggingDate = new Date();
    private String uid;
    private transient String pwd;

    LoggingInfo(String user, String password) {
        uid = user;
        pwd = password;
    }

    public String toString() {
        String password = null;
        if (pwd == null) {
            password = "NOT SET";
        } else {
            password = pwd;
        }
        return "logon info: \n   " + "user: " + uid + "\n   logging date : "
                + loggingDate.toString() + "\n   password: " + password;
    }

    public static void main(String[] args) {
       
        LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");
        System.out.println(logInfo.toString());
        try {
            ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
                    "logInfo.out"));
            o.writeObject(logInfo);
            o.close();
        } catch (Exception e) {// deal with exception

        }

        // To read the object back, we can write
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                    "logInfo.out"));
            LoggingInfo logInfo1 = (LoggingInfo) in.readObject();
            System.out.println(logInfo1.toString());
        } catch (Exception e) {// deal with exception

        }
    }
}

posted on 2011-10-14 10:02 墙头草 阅读(443) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
人人游戏网 软件开发网 货运专家