java 序列化與反序列化總結

 一.Java序列化與反序列化

 Java序列化是指把Java對象轉換爲字節序列的過程;而Java反序列化是指把字節序列恢復爲Java對象的過程。

 二.序列化與反序列化有什麼作用

序列化可以把Java對象轉換爲字節序列,然後在網絡上傳送;反序列化可以從字節序列中恢復出Java對象。

三.如何實現Java序列化與反序列化

1JDK類庫中序列化API

 java.io.ObjectOutputStream:表示對象輸出流

它的writeObject(Objectobj)方法可以對參數指定的obj對象進行序列化,把得到的字節序列寫到一個目標輸出流中。

java.io.ObjectInputStream:表示對象輸入流

它的readObject()方法源輸入流中讀取字節序列,再把它們反序列化成爲一個對象,並將其返回。

2實現序列化的要求

只有實現了Serializable或Externalizable接口的類的對象才能被序列化,否則拋出異常。該接口是一個標記式接口,它本身不包含任何內容,實現了該接口則表示這個類準備支持序列化的功能

3.java對象序列化的默認格式是二進制,當然還可以選擇其他格式,比如xml、json,下面用代碼實現以下這三種方式

實體類:Student.java

package com.gaox.entity;
/**
 * @author gaox
 * @createTime 2017年5月22日 下午2:11:50
 */

import java.io.Serializable;

public class Person implements Serializable{

	private String name;
	private String sex;
	private String address;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public Person() {
		super();
	}
	public Person(String name, String sex, String address) {
		super();
		this.name = name;
		this.sex = sex;
		this.address = address;
	}
	
	
}

第一種:默認序列化後的格式是二進制,所以在你打開person.txt發現裏面亂碼就不用奇怪了。

package com.gaox.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import com.alibaba.fastjson.JSON;
import com.gaox.entity.Person;

/**
 * @author gaox
 * @createTime 2017年5月22日 下午2:13:12
 */
public class Test {
	public static void main(String[] args) {
		Person p = new Person("明明", "女", "鄭州");
		File file = new File("e:\\person.txt");
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			//序列化
			OutputStream out=new FileOutputStream(file);
			ObjectOutputStream oops=new ObjectOutputStream(out);
			oops.writeObject(p);
			oops.flush();
			oops.close();
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {//反序列化
			InputStream in=new FileInputStream(file);
			ObjectInputStream oips=new ObjectInputStream(in);
			Person p2=(Person) oips.readObject();
			System.out.println(JSON.toJSONString(p2));
			oips.close();
			in.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

除第一種方式是jdk中自帶,其他兩種需要引入兩個架包


		com.thoughtworks.xstreamxstream1.4.9org.codehaus.jettisonjettison1.3.8

第二種方式:XML

package com.gaox.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.alibaba.fastjson.JSON;
import com.gaox.entity.Person;
import com.thoughtworks.xstream.XStream;

/**
 * @author gaox
 * @createTime 2017年5月22日 下午2:34:03
 */
public class Test2 {
	public static void main(String[] args) {
		
		XStream xstream=new XStream();
		xstream.alias("Person", Person.class);
		File file=new File("e:\\person.xml");
		FileOutputStream out = null;
		try {//序列化
			out = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		xstream.toXML(new Person("明明", "女", "鄭州"), out);
		//反序列化
		try {
			InputStream in=new FileInputStream(file);
			Person p=(Person) xstream.fromXML(file);
			System.out.println(JSON.toJSONString(p));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

結果:

<Person>

  <name>明明</name>

  <sex>女</sex>

  <address>鄭州</address>

</Person>

第三種:json

package com.gaox.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gaox.entity.Person;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;

/**
 * @author gaox
 * @createTime 2017年5月22日 下午2:46:44
 */
public class Test3 {
	public static void main(String[] args) {
		  //1.JsonHierarchicalStreamDriver  不依賴其他類庫,只實現 obj->JSON
		   // 2.JettisonMappedXmlDriver        依賴jettison類庫,實現 JSON->obj or obj->JSON
		//XStream xstream = new XStream(new JsonHierarchicalStreamDriver());
		XStream xstream = new XStream(new JettisonMappedXmlDriver());
		xstream.alias("Person", Person.class);
		File file = new File("e:\\person.json");
		try {// 序列化
			FileOutputStream out = new FileOutputStream(file);
			xstream.setMode(XStream.NO_REFERENCES);
			xstream.toXML(new Person("明明", "女", "鄭州"), out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {//反序列化
			FileInputStream in = new FileInputStream(file);
			Person p = (Person) xstream.fromXML(in);
			System.out.println(JSON.toJSONString(p));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

總結:

1)Java序列化就是把對象轉換成字節序列,而Java反序列化就是把字節序列還原成Java對象。

2)採用Java序列化與反序列化技術,一是可以實現數據的持久化,在MVC模式中很是有用;二是可以對象數據的遠程通信。


 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章