java 序列化与反序列化总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fox9916/article/details/72627218

 一.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.9
     
     org.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模式中很是有用;二是可以对象数据的远程通信。


猜你喜欢

转载自blog.csdn.net/fox9916/article/details/72627218