J2SE中的序列化的認(rèn)識(shí)
只要我們的class 實(shí)現(xiàn)了java.io.Serializable接口,就可以利用ObjectOutputStream的writeObject()方法將一個(gè)對(duì)象序列化;利用ObjectInputStream的readObject()方法,可以返回讀出的object對(duì)象。Serializable接口不需要我們實(shí)現(xiàn)任何方法。
以下是一個(gè)例子,它能給我們一個(gè)感性的認(rèn)識(shí):
Serial實(shí)現(xiàn)了就java.io.Serializable接口,是需要序列化的類。我們首先構(gòu)造一個(gè)Serial的對(duì)象serial1然后將其保存(序列化)在一個(gè)文件中,而后再將其讀出(反序列化),并打印其內(nèi)容。
package Stream;
/**
* @author favo yang
*/
import java.io.*;
public class Serial implements Serializable {
int company_id;
String company_addr;
boolean company_flag;
public Serial(){}//不同于c++,沒有也可以
public Serial(int company_id,String company_addr,boolean company_flag) {
this.company_id=company_id;
this.company_addr=company_addr;
this.company_flag=company_flag;
}
public static void main(String[] args) {
Serial serial1 = new Serial(752,"dayer street #5 building 02-287",false);//構(gòu)造一個(gè)新的對(duì)象
FileInputStream in=null;
FileOutputStream out=null;
ObjectInputStream oin=null;
ObjectOutputStream oout=null;
try {
out = new FileOutputStream("5.txt");
oout = new ObjectOutputStream(out);
serial1.serialize(oout);//序列化
oout.close();
oout=null;
in = new FileInputStream("5.txt");
oin = new ObjectInputStream(in);
Serial serial2 = Serial.deserialize(oin);//反序列化
System.out.println(serial2);//打印結(jié)果
} catch (Exception ex){
ex.printStackTrace();
} finally{
try {
if (in != null) {
in.close();
}
if (oin != null) {
oin.close();
}
if (out != null) {
out.close();
}
if (oout != null) {
oout.close();
}
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
/**
* deserialize
*/
public static Serial deserialize(ObjectInputStream oin) throws Exception{
Serial s=(Serial)oin.readObject();
return s;
}
public String toString() {
return "DATA: "+company_id+" "+company_addr+" "+company_flag;
}
/**
* serialize
*/
public void serialize(ObjectOutputStream oout) throws Exception{
oout.writeObject(this);
}
}
運(yùn)行結(jié)果:
DATA: 752 dayer street #5 building 02-287 false
正確打印了結(jié)果。
- J2EE 開發(fā)購物網(wǎng)站 經(jīng)驗(yàn)篇 - 建表
- [J2SE]Java中3DES加密解密調(diào)用示例
- J2ME/J2EE實(shí)現(xiàn)用戶登錄交互 實(shí)現(xiàn)代碼
- JAVA幫助文檔全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版整理
- J2SE與c#的幾點(diǎn)比較
- J2EE項(xiàng)目代碼編寫規(guī)范分享
- Eclipse配置Tomcat和JDK步驟圖解
- 在Ubuntu系統(tǒng)下安裝JDK和Tomcat的教程
- J2ME編程中的幾個(gè)重要概念介紹
- JDK、J2EE、J2SE、J2ME四個(gè)易混淆概念區(qū)分
相關(guān)文章
tomcat6下jsp出現(xiàn)getOutputStream() has already been called for t
這篇文章主要介紹tomcat6下jsp出現(xiàn)getOutputStream() has already been called for this response異常的原因和解決方法,需要的朋友可以參考下。2016-06-06
jsp中點(diǎn)擊圖片彈出文件上傳界面及預(yù)覽功能的實(shí)現(xiàn)
點(diǎn)擊圖片彈出文件上傳界面的效果,想必大家都有見到過吧,在本文為大家詳細(xì)介紹下在jsp中是如何實(shí)現(xiàn)的,并對(duì)具體的實(shí)現(xiàn)代碼做簡(jiǎn)要的介紹,感興趣的朋友不要錯(cuò)過2013-10-10
JSP實(shí)現(xiàn)登錄功能之添加驗(yàn)證碼
jsp登陸驗(yàn)證,網(wǎng)頁登陸驗(yàn)證帶驗(yàn)證碼校驗(yàn),登錄功能之添加驗(yàn)證碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12
JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例
這篇文章主要介紹了JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例,本文重點(diǎn)在JSP代碼示例中,需要的朋友可以參考下2014-09-09
使用IDEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法
在使用IDEA開發(fā)maven+springMVC項(xiàng)目時(shí)遇到不加載EL表達(dá)式的問題,遇到此問題的朋友不在少數(shù),今天小編給大家?guī)砹耸褂肐DEA編寫jsp時(shí)EL表達(dá)式不起作用的問題及解決方法,一起看看吧2018-08-08
搭建Eclipse+MyEclipse開發(fā)環(huán)境
搭建Eclipse+MyEclipse開發(fā)環(huán)境...2007-02-02
jsp實(shí)現(xiàn)針對(duì)excel及word文檔的打印方法
這篇文章主要介紹了jsp實(shí)現(xiàn)針對(duì)excel及word文檔的打印方法,涉及JSP操作excel及word的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
jsp+ajax實(shí)現(xiàn)無刷新(鼠標(biāo)離開文本框即驗(yàn)證用戶名)實(shí)現(xiàn)思路
jsp+ajax實(shí)現(xiàn)無刷新,鼠標(biāo)離開文本框即驗(yàn)證用戶名,很方便的功能,感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)ajax無刷新有所幫助2013-01-01

