码迷,mamicode.com
首页 > 其他好文 > 详细

ObjectOutputStream 对象追加的实现

时间:2015-01-23 18:27:41      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

<pre class="java" name="code">	/**
	 * 将单个对象写入文件
	 * 
	 * @param ls
	 */
	public void writeObject(Sgin sgin) throws IOException {
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

			try {
				File saveFile = new File(this.filename);
				oos = AppendObjectOutputStream.newInstance(saveFile);
				oos.writeObject(sgin);
				oos.flush();
			} catch (IOException e) {
				throw e;
			} finally {
				if (null != oos) {
					oos.close();
				}
				if(null !=fos){
					fos.close();
				}
			}
		} else {
			throw new RuntimeException("Please insert the SD card");
		}

	}


package com.ztesoft.mfp.common.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * 追加模式的对象流
 * 使用 newInstance 静态 方法
 * @author wanglei
 * 
 */
public class AppendObjectOutputStream extends ObjectOutputStream {

	/**
	 * 追加模式的对象流
	 * @param	File
	 * @return	ObjectOutputStream
	 * @throws	IOException
	 */
	public static ObjectOutputStream newInstance(File file) throws IOException {
		long length = file.length();
		FileOutputStream fos = new FileOutputStream(file,true);
		ObjectOutputStream oos = null;
		if(length>0){
			oos = new AppendObjectOutputStream(fos);
		}else{
			oos = new ObjectOutputStream(fos);
		}
		return oos ;
	}

	private AppendObjectOutputStream(OutputStream out) throws IOException {
		super(out);
	}

	
	@Override
	protected void writeStreamHeader() throws IOException {
		super.reset();
	}
}

ObjectOutputStream 对象追加的实现

标签:

原文地址:http://blog.csdn.net/wkwanglei/article/details/43057639

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!