1.什么是序列化?
序列化就是将JVM当中正在运行的信息以字节信息表示
2.对象序列化?
对象序列化就是JVM中的对象信息(static ,transient 修饰的变量不会被序列化)以字节形式展示,使用ObjectInputStream或ObjectOutputStream操作实体类时应实现Serializable接口,使用XMLEncoder或XMLDecoder时,可以不实现该接口
3.什么时候使用序列化?
对象数据的持久化或对象数据的网络传输
下面就几个例子进行介绍:
一.进行序列化:
1.使用ObjectOutputStream序列化
/**
* @param obj
* 需要被序列化的对象
* @param uri
* 序列化存储的目标位置包括文件名
* @return 将存储的结果文件返回
*/
public static File writeObject(List<Object> list, String uri)
{
File file = new File(uri);
ObjectOutputStream out = null;
OutputStream fout = null ;
try
{
if (!file.exists())
file.createNewFile();
fout = new FileOutputStream(file,true);
if(file.length() < 1){
out = new ObjectOutputStream(fout);
}else{
out = new MyObjectOutputStream(fout);
}
for(Object obj : list){
out.writeObject(obj);
}
out.flush();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
if (out != null)
{
try
{
out.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
return file;
}
//支持多次写入序列化结果写入同一文件
static class MyObjectOutputStream extends ObjectOutputStream{
protected MyObjectOutputStream() throws IOException, SecurityException
{
super();
}
public MyObjectOutputStream(OutputStream out) throws IOException {
super(out);
}
@Override
protected void writeStreamHeader() throws IOException {
return;
}
}
2.使用XMLEncoder序列化
/**@param uri 目标文件路径及文件名
* @param obj 需要被序列化的对象
* @return 将序列化后的结果返回
* 使用java.beans.XMLEncode的类将对象序列化为XML格式的文件*/
public static File xmlEncode(String uri ,List<Object> list){
File file = new File(uri);
XMLEncoder encode = null ;
try{
encode = new XMLEncoder(new FileOutputStream(file,true),"utf-8",false,0);
for(Object each : list){
encode.writeObject(each);
}
encode.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(encode != null){
encode.close();
}
}
return file;
}
二.反序列化
1.使用ObjectInputStream
/**
* @param file 需要反序列化的文件
* @return person 反序列化后得到对象,反序列化是不会调用该类的构造器
*/
public static List<Object> readObject(File file)
{
ObjectInputStream in = null;
List<Object> list = new ArrayList<Object>();
try
{
in = new ObjectInputStream(new FileInputStream(file));
while(true){
Object obj = in.readObject();
list.add(obj);
}
} catch ( ClassNotFoundException e)
{
e.printStackTrace();
}
catch(EOFException e){
System.out.println("文件到达末尾");
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}finally
{
// 关闭资源
if (in != null)
{
try
{
in.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return list;
}
2.使用XMLDecoder
/**
* @param uri 将uri目标位置文件反序列化为一个对象
* @return 将反序列后的结果返回,会调用该类的无参构造函数,当该类没有无参的构造函数时,抛出java.lang.InstantiationException异常
* 所以要使用该方法进行反序列化时该类需要有无参的构造方法
* */
public static List<Object> xmlDecode(String uri ){
XMLDecoder decode = null ;
File file = new File(uri);
List list = new ArrayList();
try{
FileInputStream fin = new FileInputStream(file);
Object obj = null ;
decode = new XMLDecoder(fin);
while((obj = decode.readObject()) != null){
list.add(obj);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e)
{
e.printStackTrace();
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}finally{
if(decode != null){
decode.close();
}
}
return list ;
}
附加:本次试用的实体类
public class Person implements Serializable
{
/**
* @author 郭暸
*
*
*/
private static final long serialVersionUID = -6922946081862337047L;
private String name ;
private transient Sex sex;
private int age ;
public Person(){
}
public String getName()
{
return name;
}
public Sex getSex()
{
return sex;
}
public int getAge()
{
return age;
}
public void setName(String name)
{
this.name = name;
}
public void setSex(Sex sex)
{
this.sex = sex;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString(){
return "name:"+this.name+"\tage:"+this.age+"\tsex:"+this.sex;
}
}