<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();
}
}