public class Test6 implements Serializable{ private static final long serialUID = 1L; private Date loggingDate = new Date(); private String uid ; private transient String pwd ; Test6(String user ,String pwd ){ this.uid = user ; this.pwd = pwd; } public String toString (){ String password = null ; if (pwd== null){ password = "NOT SET"; }else { password = pwd; } return "logon info:"+uid+","+password; } public static void main(String [] args){ Test6 logInfo = new Test6("lsj", "12345"); System.out.println(logInfo.toString()) ; try { ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("logInfo.out")); os.writeObject(logInfo); os.close() ; } catch (Exception e) { // TODO: handle exception } try { ObjectInputStream is = new ObjectInputStream(new FileInputStream("logInfo.out")); Test6 logInfo1= (Test6) is.readObject(); System.out.println(logInfo1.toString()); } catch (Exception e) { // TODO: handle exception } } }
结果
logon info:lsj,12345
logon info:lsj,NOT SET