解决方案 »
- java里面如何停止一个运行的线程?
- 求run.bat文件写运行jar包,依赖的jar包如何写?
- java.util.Vector类求解
- 怎么样判断某个类是否是另一个类的子类???
- 将CheckBox作为单元格Editor的表格列如何修改其值?
- 如何format一个16进制的串
- HibernateOperations saveOrUpdate(entity) 不能触发SQL语句
- 谁能给我讲讲“JTextPane中设置局部文本(例如关键字)的Color”的工作原理
- java 中Collections排序问题!!(附源码)
- java中窗口最大化于窗口最小化事件好象不能触发,是不是我搞错了,忘给给个例子
- 输入输出流的问题,求大神帮忙
- 怎么获取一个博客页面<div>文字<div>之间的文字
demo只是很个很烂的演示,期待高手的建议。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;class Person implements Serializable{
private static final long serialVersionUID = -1725073815168650916L;
private int id;
private String name;
public Person(int id, String name){
this.id = id;
this.name = name;
}
@Override
public String toString() {
return String.format("id=[%d], name=[%s]%n", this.id, this.name);
}
}
public class Demo { /**
* 将Object对象存入数组,接着存入文件
*
* Object可以为任意对象,本demo为Person
*
* @param objects 需要存储的对象数组
* @param fileName 存储文件名
*
* @throws FileNotFoundException
* @throws IOException
*/
public static void saveObject(Object[] objects, String fileName) throws FileNotFoundException, IOException{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName, true));
// 在文件头写1个int,表示存储了多少个对象
int numOfObject = objects.length;
out.writeInt(numOfObject);
for (Object obj : objects) {
out.writeObject(obj);
}
System.out.println(String.format("%d objects saved", numOfObject));
}
/**
* 从存储文件命中读取对象
*
* @param fileName 存储文件名
* @return Object[] 从文件中读取返回的对象数组
*
* @throws FileNotFoundException
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object[] loadObject(String fileName) throws FileNotFoundException, IOException, ClassNotFoundException{
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
// 从文件头获取需要读入的对象个数
int numOfObject = in.readInt();
System.out.println(String.format("%d objects need to be loaded", numOfObject));
Object[] objects = new Object[numOfObject];
for(int i=0;i<numOfObject;i++){
objects[i] = in.readObject();
}
return objects;
}
public static void main(String[] args) {
Object[] person = new Object[1500];
for(int i=0;i<person.length;i++){
person[i] = new Person(i, "name"+i);
}
try {
saveObject(person, "d:/person.dat");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
for (Object p : loadObject("d:/person.dat")) {
System.out.println((Person)p);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
if (fileSize > 0) {
oos = new ObjectOutputStream(new FileOutputStream("E:/a.dat",
true)) {
// 重写 writeStreamHeader()方法,空实现
protected void writeStreamHeader() {
};
};
} else {
oos = new ObjectOutputStream(new FileOutputStream("E:/a.dat",
true));
}
你执行3次之后,30个对象以经全写进去了。(或者,你可以右击属性查看下"E:/a.dat"的字节量,是否增加了)
http://www.cnblogs.com/elleniou/archive/2012/08/29/2662353.html
还有就是楼主你的异常捕捉完至少要print下不然你都不知道其实程序抛异常了