Java對(duì)象序列化就那些實(shí)現(xiàn)了Serializable接口的對(duì)象轉(zhuǎn)換成一個(gè)字節(jié)序列,并能夠在以后將這個(gè)字節(jié)序列 完全恢復(fù)為原來(lái)的對(duì)象。這一過(guò)程甚至可以通過(guò)網(wǎng)絡(luò)進(jìn)行;運(yùn)行Windows操作系統(tǒng)的計(jì)算機(jī)上創(chuàng)建的一個(gè)對(duì)象將其序列化,通過(guò)網(wǎng)絡(luò)將它發(fā)送到一臺(tái)運(yùn)行 Unix系統(tǒng)的計(jì)算機(jī),然后在那里準(zhǔn)確地重新組裝。Java序列化的一個(gè)引用就是RMI,當(dāng)向遠(yuǎn)程對(duì)象發(fā)送消息,需要通過(guò)對(duì)象序列化來(lái)傳輸參數(shù)和返回值。 PS:通過(guò)序列化之后我們可以將對(duì)象保存到持久化的設(shè)備如文件、數(shù)據(jù)庫(kù)
實(shí)現(xiàn): 實(shí)現(xiàn)Serializable接口。這個(gè)接口是標(biāo)記接口里面沒(méi)有什么方法要實(shí)現(xiàn)的!然后使用一個(gè)輸出流(如:FileOutputStream) 構(gòu)造一個(gè)ObjectOutputStream(對(duì)象流)。然后用ObjectOutputStream的writeObject(Object obj)方法就可以將參數(shù)為obj 的對(duì)象寫(xiě)出保存其狀態(tài)。如果要恢復(fù)的話(huà)就使用輸入流實(shí)現(xiàn)!
1. 數(shù)據(jù)表結(jié)構(gòu) CREATE TABLE `obj` ( `id` int(11) NOT NULL auto_increment, `object` blob, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8; 2. 讀寫(xiě)代碼 package org.test; import java.io.*; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement;
public class ObjectSerial { public static void main(String[] args) { //創(chuàng)建測(cè)試用對(duì)象 City beijing.setName("北京"); beijing.setCode("010"); City shanghai = shanghai.setName("上海"); shanghai.setCode("020"); City tianjin.setName("天津"); tianjin.setCode("021"); List<City> cityList = new ArrayList<City>(); cityList.add( cityList.add(shanghai); cityList.add( TestObject obj = new TestObject(); obj.setName("yangsq"); obj.setPassword("111"); obj.setDate(new Date()); obj.setCityList(cityList); try{ //將對(duì)象存入blob字段 Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "321"); con.setAutoCommit(false); //將一個(gè)對(duì)象序列化保存到數(shù)據(jù)庫(kù) PreparedStatement pstmt = con.prepareStatement("insert into obj (object) values (?)"); pstmt.setObject(1, obj); pstmt.executeUpdate(); con.commit(); //從數(shù)據(jù)庫(kù)中提取記錄 Statement state = con.createStatement(); ResultSet rs = state.executeQuery("select object from obj"); if (rs.next()) { //以下是讀取的方法一定要注意了! Blob inblob = (Blob) rs.getBlob("object"); InputStream is = inblob.getBinaryStream(); BufferedInputStream input = new BufferedInputStream(is); byte[] buff = new byte[(int) inblob.length()];//放到一個(gè)buff 字節(jié)數(shù)組 while(-1 != (input.read(buff, 0, buff.length))); ObjectInputStream in =new ObjectInputStream(new ByteArrayInputStream(buff)); TestObject w3 = (TestObject)in.readObject();//從IO流中讀取出來(lái).可以得到一個(gè)對(duì)象了 System.out.println(w3.getName()); System.out.println(w3.getPassword()); System.out.println(w3.getDate()); } } catch (Exception ex) { ex.printStackTrace(); System.exit(1); } } }
class TestObject implements Serializable { private static final long serialVersionUID = 4558876142427402513L; /** * @param args */ private String name; private String password; private Date date; private List<City> cityList; public List<City> getCityList() { return cityList; } public void setCityList(List<City> cityList) { this.cityList = cityList; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } class City implements Serializable{ private static final long serialVersionUID = 4558876127402513L; private String name; private String code; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
|