Java中ObjectInputStream 与 ObjectOutputStream的使用

本文介绍如何使用Java的ObjectInputStream和ObjectOutputStream进行对象的序列化与反序列化,包括将对象写入文件及从文件中读取对象的方法,并提供了一个简单的示例。

ObjectInputStream能够让你从输入流中读取Java对象,而不需要每次读取一个字节。你可以把InputStream包装到ObjectInputStream中,然后就可以从中读取对象了

ObjectOutputStream能够让你把对象写入到输出流中,而不需要每次写入一个字节。你可以把OutputStream包装到ObjectOutputStream中,然后就可以把对象写入到该输出流中了

ObjectInputStream和ObjectOutputStream还有许多read和write方法,比如readInt、writeLong等等,详细信息请查看 http://docs.oracle.com/javase/7/docs/api/

这里操作的bean 必须要序列化 至于序列化是啥就自己去百度吧  implements Serializable

首先说 ObjectOutputStream 把对象转成文件 

	    public static void objectToFile(Object obj, String outputFile) {  
	        ObjectOutputStream oos = null;  
	        FileOutputStream fos = null;  
	        try {  
	            fos = new FileOutputStream(new File(outputFile));  
	            oos = new ObjectOutputStream(fos);  
	          //开始写入  
	            oos.writeObject(obj);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            if (oos != null) {  
	                try {  
	                    oos.close();  
	                } catch (IOException e1) {  
	                    e1.printStackTrace();  
	                }  
	            }  
	            if (fos != null) {  
	                try {  
	                    fos.close();  
	                } catch (IOException e2) {  
	                    e2.printStackTrace();  
	                }  
	            }  
	        }  
	    }  

ObjectInputStream就读出bean了 直接用bean接收就行了

	 public static Object fileToObject(String fileName) {  
		  
	        FileInputStream fis = null;  
	        ObjectInputStream ois = null;  
	        try {  
	            fis = new FileInputStream(fileName);  
	            ois = new ObjectInputStream(fis);  
	            Object object = ois.readObject();  
	            return object;  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } finally {  
	            if (fis != null) {  
	                try {  
	                    fis.close();  
	                } catch (IOException e1) {  
	                    e1.printStackTrace();  
	                }  
	            }  
	            if (ois != null) {  
	                try {  
	                    ois.close();  
	                } catch (IOException e2) {  
	                    e2.printStackTrace();  
	                }  
	            }  
	        }  
	        return null;  
	    }  

很简单的几个方法调用  

使用:

实体:

public class A implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8425494920969357915L;
	public String n = "哈哈";

	public String getN() {
		return n;
	}

	public void setN(String n) {
		this.n = n;
	}

	public A(String n) {
		super();
		this.n = n;
	}

	public A() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public String toString() {
		return "A [n=" + n + "]";
	}
	
	
	
}

感觉这个在项目中还是有用的,有疑问可以加上面的qq群相互学习哦~~
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值