如何把对象持久化

这里我来介绍如何把对象也持久化

1、ObjectInputStream :对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。

2、ObjectOutputStream:。通过在流中使用文件可以实现对象的持久存储,只能将支持 java.io.Serializable 接口的对象写入流中

 

接下来看我们如何来编写这样的程序:

1、新建一个被系列化的对象:PersistenceObject.java

    

package com.mars.object;

import java.io.Serializable;

public class PersistenceObject implements Serializable{
	private static final long serialVersionUID = 1L;
	private String name;

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

 

 

2、创建一个类来专门的设置文件路径:FileLocal.java

 

   

package com.mars.object;

public final class FileLocal {
	protected static final String FILELOCAL="D:/object.tmp";
}

 

 

3、接下来建立一个类来持久化这个对象:OutPersistenceObject .java

package com.mars.object;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class OutPersistenceObject {
	public static void outObject(){
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream(FileLocal.FILELOCAL);//保存要持久的介质
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		if(fos!=null){
			try {
				oos=new ObjectOutputStream(fos);
				oos.writeObject(com.mars.object.PersistenceObject.class.newInstance());//把PersistenceObject类持久化
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}finally{
				if(oos!=null){
					try {
						oos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}finally{
						oos=null;
					}	
				}
				if(fos!=null){
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}finally{
						fos=null;
					}
				}
			}
		}
	}
	
	public static void main(String[] args) {
		System.out.println("开始存入D:/object.tmp.....");
		OutPersistenceObject.outObject();
		System.out.println("持久化完成....");
	}
}

 

 

4、如何把这个持久化的对象从存储介质中读取出来:InPersistenceObject .java

package com.mars.object;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class InPersistenceObject {	
	public static Object getObject(String src){
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		Object o = null;
		try {
			fis = new FileInputStream(src);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		try {
			ois = new ObjectInputStream(fis);
			o = ois.readObject();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(ois!=null){
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					ois=null;
				}	
			}
			if(fis!=null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					fis=null;
				}
			}
		}
		return o;
	}
	public static void main(String[] args) {
		PersistenceObject p=(PersistenceObject)InPersistenceObject.getObject(FileLocal.FILELOCAL);
		p.setName("hello ! I'm mars ");
		String str = p.getName();
		System.out.println(str);
	}

}

 

 

end....

你可能感兴趣的:(java)