OnJava8-Examples/io/Logon.java
Bruce Eckel 49edcc8b17 reorg
2015-06-15 17:47:35 -07:00

48 lines
1.4 KiB
Java

//: io/Logon.java
// ©2015 MindView LLC: see Copyright.txt
// Demonstrates the "transient" keyword.
import java.util.concurrent.*;
import java.io.*;
import java.util.*;
import static com.mindviewinc.util.Print.*;
public class Logon implements Serializable {
private Date date = new Date();
private String username;
private transient String password;
public Logon(String name, String pwd) {
username = name;
password = pwd;
}
@Override
public String toString() {
return "logon info: \n username: " + username +
"\n date: " + date + "\n password: " + password;
}
public static void main(String[] args) throws Exception {
Logon a = new Logon("Hulk", "myLittlePony");
print("logon a = " + a);
try(ObjectOutputStream o = new ObjectOutputStream(
new FileOutputStream("Logon.out"))) {
o.writeObject(a);
}
TimeUnit.SECONDS.sleep(1); // Delay
// Now get them back:
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("Logon.out"));
print("Recovering object at " + new Date());
a = (Logon)in.readObject();
print("logon a = " + a);
}
} /* Output:
logon a = logon info:
username: Hulk
date: Mon Jun 15 15:47:51 PDT 2015
password: myLittlePony
Recovering object at Mon Jun 15 15:47:52 PDT 2015
logon a = logon info:
username: Hulk
date: Mon Jun 15 15:47:51 PDT 2015
password: null
*///:~