只要你的类实现了Serializable接口
在需要的时候就可以直接写到文件中
ObjectOutputStream oos = new ObjectOutStream(new FileOutputStream("c:\\obj.dat"));
oos.writeObject(vect);
oos.close();
以后就使用ObjectInputStream读回来
在需要的时候就可以直接写到文件中
ObjectOutputStream oos = new ObjectOutStream(new FileOutputStream("c:\\obj.dat"));
oos.writeObject(vect);
oos.close();
以后就使用ObjectInputStream读回来
解决方案 »
- 关于JDBC连MSSQL的一个超奇怪的问题,高手看看
- 初学JSP ,一个有关浏览器与服务器时间的问题!
- 我的项目在我机子上可以访问,为什么局域网的其他电脑访问不了呢????
- 如何指定div中滚动块在滚动条中的初始位置
- TOMCAT 出错,又来麻烦大家了.......
- struts2 datetimepicker属性事件无效
- Jsp文件为什么用UltraEdit打不开??右键直接UltraEdit可以,但选择程序选UE便打不开!
- 熟悉ASP的大哥,请进来谈一谈JSP?
- 【求助】jsp中的include指令能不能包含xml文件?
- 关于自建目录放置servlet的问题
- sun.servlet.http包的下载
- 各位大帅呀!帮帮我吧!ie漏洞的!!!!
try {
ois = new ObjectInputStream(new FileInputStream("D:\cache.txt")) ;
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} try {
Vect c = (Vect )ois.readObject() ;
System.out.println(c.getCode());
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}这样可以读取出一个来,但是如何把多个对象给还原回来
try {
ois = new ObjectInputStream(new FileInputStream("D:\cache.txt")) ;
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} try {
//假定前面你已经写了5000个Vector对象进文件里
List list = new LinkedList();
for(int i = 0; i < 5000; i++){
Vect c = (Vect )ois.readObject() ;
list.add(c);
System.out.println(c.getCode());
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}
不断的readObject就可以知道最后有多少个啦,到达最后java会抛出IOException的啦