我的java發跡史
常用鏈接
我的隨筆
我的評論
我的參與
最新評論
留言簿
(3)
給我留言
查看公開留言
查看私人留言
隨筆檔案
2007年5月 (8)
2007年4月 (2)
搜索
最新評論
1.?re: Java中的transient[未登錄]
看來我對持久化和序列化的了解連皮毛都算不上阿,這個例子簡單明了,很好,不過這個blog對code的支持不夠友好阿,多謝博主
--Merlin
2.?re: Java中的transient
一看例子就明白了,寫的挺好的。
--風嗜塵
3.?re: Java中的transient[未登錄]
@cheng
很好很強大
--test
4.?re: Java中的transient
例子挺好,明白了,謝啦@cheng
--了了
5.?re: Java中的transient
寫的很好。切中要點
--了凡
閱讀排行榜
1.?Java中的transient(20594)
2.?取模不是取余(2867)
3.?spring 事務的 自動裝配(1414)
4.?大數階乘算法~~~(792)
5.?quickSort算法~~(621)
評論排行榜
1.?Java中的transient(6)
2.?spring 事務的 自動裝配(4)
3.?計劃~~(1)
4.?取模不是取余(0)
5.?泛型的獲取class(0)
Powered by:
博客園
模板提供:
滬江博客
BlogJava
|
首頁
|
發新隨筆
|
發新文章
|
聯系
|
聚合
|
管理
Java中的transient
Java中的transient,看jdk源碼的時候突然忘了這個是什么了,查了一下,是用于聲明序列化的時候不被存儲的,在這里記下
發表于 2007-04-22 12:35
劉甘泉
閱讀(20594)
評論(6)
編輯
收藏
評論
#
re: Java中的transient[未登錄]
回復
更多評論
import java.io.*;
import java.util.*;
class Logon implements Serializable {
private Date date = new Date();
private String username;
private transient String password;
Logon(String name, String pwd) {
username = name;
password = pwd;
}
public String toString() {
String pwd =
(password == null) ? "(n/a)" : password;
return "logon info: \n " +
"username: " + username +
"\n date: " + date.toString() +
"\n password: " + pwd;
}
public static void main(String[] args) {
Logon a = new Logon("Hulk", "myLittlePony");
System.out.println( "logon a = " + a);
try {
ObjectOutputStream o =
new ObjectOutputStream(
new FileOutputStream("Logon.out"));
o.writeObject(a);
o.close();
// Delay:
int seconds = 5;
long t = System.currentTimeMillis()
+ seconds * 1000;
while(System.currentTimeMillis() < t)
;
// Now get them back:
ObjectInputStream in =
new ObjectInputStream(
new FileInputStream("Logon.out"));
System.out.println(
"Recovering object at " + new Date());
a = (Logon)in.readObject();
System.out.println( "logon a = " + a);
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
cheng
評論于 2009-02-23 20:33
#
re: Java中的transient
回復
更多評論
寫的很好。切中要點
了凡
評論于 2009-12-02 08:53
#
re: Java中的transient
回復
更多評論
例子挺好,明白了,謝啦@cheng
了了
評論于 2010-07-26 09:44
#
re: Java中的transient[未登錄]
回復
更多評論
@cheng
很好很強大
test
評論于 2011-08-09 14:26
#
re: Java中的transient
回復
更多評論
一看例子就明白了,寫的挺好的。
風嗜塵
評論于 2012-02-13 14:55
#
re: Java中的transient[未登錄]
回復
更多評論
看來我對持久化和序列化的了解連皮毛都算不上阿,這個例子簡單明了,很好,不過這個blog對code的支持不夠友好阿,多謝博主
Merlin
評論于 2012-09-12 13:37
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
主站蜘蛛池模板:
英吉沙县
|
兴海县
|
贵州省
|
绥宁县
|
长葛市
|
涿鹿县
|
保康县
|
乡宁县
|
平原县
|
惠东县
|
清涧县
|
丁青县
|
泊头市
|
图们市
|
邵阳县
|
孝感市
|
沭阳县
|
泰来县
|
昌乐县
|
射阳县
|
泾源县
|
合山市
|
长兴县
|
永昌县
|
剑阁县
|
含山县
|
城固县
|
应城市
|
东台市
|
安岳县
|
昌图县
|
新化县
|
永德县
|
肇源县
|
塔城市
|
惠安县
|
凤山县
|
斗六市
|
崇文区
|
前郭尔
|
稷山县
|
import java.util.*;
class Logon implements Serializable {
private Date date = new Date();
private String username;
private transient String password;
Logon(String name, String pwd) {
username = name;
password = pwd;
}
public String toString() {
String pwd =
(password == null) ? "(n/a)" : password;
return "logon info: \n " +
"username: " + username +
"\n date: " + date.toString() +
"\n password: " + pwd;
}
public static void main(String[] args) {
Logon a = new Logon("Hulk", "myLittlePony");
System.out.println( "logon a = " + a);
try {
ObjectOutputStream o =
new ObjectOutputStream(
new FileOutputStream("Logon.out"));
o.writeObject(a);
o.close();
// Delay:
int seconds = 5;
long t = System.currentTimeMillis()
+ seconds * 1000;
while(System.currentTimeMillis() < t)
;
// Now get them back:
ObjectInputStream in =
new ObjectInputStream(
new FileInputStream("Logon.out"));
System.out.println(
"Recovering object at " + new Date());
a = (Logon)in.readObject();
System.out.println( "logon a = " + a);
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
很好很強大