hashMap的反序列化问题

Evi 2012-05-06 05:09:44
这是我的hashMap

Map<Integer,Student> students=new HashMap<Integer,Student>();

当我向里面添加数据完了以后用:

stuInfo = new File("stuinfo.ser");
oos = new ObjectOutputStream( new FileOutputStream(stuInfo));
oos.writeObject(students);
oos.close();

将students序列化存在stuinfo.ser中没有问题,当我关闭程序重新读取时:

ois= new ObjectInputStream(new FileInputStream(stuInfo));
students=(Map<Integer, Student>) ois.readObject();

程序出现java.io.EOFException

当我没有关闭程序时在反序列化就可以,这是为什么?怎么解决!

我的目的是是想把在程序关闭时之前students保存下来,当下次打开程序再将它读取出来

求指教!
...全文
938 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdojqy1122 2012-05-06
  • 打赏
  • 举报
回复
还没开始操作数据就打开两接口堵上。。。向我那样分开业务逻辑。
Evi 2012-05-06
  • 打赏
  • 举报
回复
原来是这样的啊,学习了
感谢!感谢!每次出问题都是CSDN上给解决的,

最后祝各位身体健康,合家幸福!
MiceRice 2012-05-06
  • 打赏
  • 举报
回复
另外,想说明一下:

对于文件操作,或者说所有资源类操作吧,有类似数据库连接使用的准则:

尽量晚打开,尽量早释放。
MiceRice 2012-05-06
  • 打赏
  • 举报
回复
你真狠。。。看看你的构造函数:
Test(File stuinfo){
this.stuinfo=stuinfo;
try {
oos = new ObjectOutputStream(new FileOutputStream(stuinfo));

直接就用把FileOutputStream这个文件的内容清空了。。。
Evi 2012-05-06
  • 打赏
  • 举报
回复
import java.io.File;
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.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

/**
* 用于测试HashMap的序列化问题
* @author Evi
*/

public class Test {
File stuinfo;//存放hashmap序列化后的文件
ObjectOutputStream oos = null;
ObjectInputStream ois = null;


/**
* 构造函数
* @param stuinfo
* File 存放HashMap<Integer,Student>
*/
Test(File stuinfo){
this.stuinfo=stuinfo;
try {
oos = new ObjectOutputStream(new FileOutputStream(stuinfo));
ois = new ObjectInputStream(new FileInputStream(stuinfo));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


/**
* 将sourstudents写入文件stuinfo.ser
* @param sourstudents
* Map<Integer,Student>
*/
public void write(Map<Integer,Student> sourstudents){
try {
oos.writeObject(sourstudents);
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 从stuinfo.ser读取出数据存放在deststudents (HashMap<Integer,Student>)中
* @return deststudents
* Map<Integer,Student> 返回从stuinfo.ser文件中读取出来的deststudents
*/
public Map<Integer,Student> read(){
Map<Integer,Student> deststudents = null;
try {
deststudents=(Map<Integer, Student>) ois.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return deststudents;
}

/**
* 显示students中存放的学生信息
* @param sourstudents2
* Integer,Student> students
*/
public void display(Map<Integer, Student> students){
Iterator<Entry<Integer,Student>> iter = students.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<Integer, Student> entry = iter.next();
Student stu = entry.getValue();
System.out.println(" 学号:" + stu.getId() + " 姓名:"+ stu.getName());
}
}

public static void main(String[] args) {
File stuinfo=new File("stuinfo.ser");
//由stuinfo.ser读取出来的HashMap<Integer,Student>
Map<Integer,Student> deststudents=new HashMap<Integer,Student>();
/*------------初始化一个HashMap<Integer,Student>----------------*/
Map<Integer,Student> sourstudents=new HashMap<Integer,Student>();
Student stu1 = new Student(Integer.valueOf(3),"张三");
Student stu2 = new Student(Integer.valueOf(8),"李四");
sourstudents.put(stu1.getId(), stu1);
sourstudents.put(stu2.getId(), stu2);
Test test=new Test(stuinfo);

/*-----------第一次执行的代码-----------------*/
//显示students中的学生信息
test.display(sourstudents);
//将初始化好的students (HashMap<Integer,Student>)写入stuinfo.ser文件中
test.write(sourstudents);

/*-------------第二次执行的代码---------------*/
//从stuinfo.ser文件中读数据将其存放在test对象的deststudents(HashMap<Integer,Student>)中
deststudents=test.read();
test.display(deststudents);

/*
* 说明:
* 第一次运行程序将第二次执行的代码注释,执行第一次执行的代码 程序正常运行
* --- 这时已经将sourstudents写入文件stuinfo.ser中
*
*
* 第二次运行程序注释第一次执行的代码执行第二次的代码程序报错java.io.EOFException
* ----从stuinfo.ser读取数据报错为什么
*
*
* 程序一次运行两段代码正常
*/




}

}


这是我简化后的代码
sdojqy1122 2012-05-06
  • 打赏
  • 举报
回复
如下是测试代码,没有问题,我是疑惑楼主代码或许有问题。

import java.io.File;
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;
import java.util.HashMap;
import java.util.Map;

public class Test {
public static void main(String args[]) {
writer();//自己可以注释掉代码试试
reader();
}
public static void writer(){
Map<Integer,Student> students=new HashMap<Integer,Student>();
students.put(1, new Student("A"));
students.put(2, new Student("B"));
File stuInfo = new File("stuInfo.txt");
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream( new FileOutputStream(stuInfo));
oos.writeObject(students);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
oos.flush();
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void reader(){
Map<Integer,Student> students= null;
File stuInfo = new File("stuInfo.txt");
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(stuInfo));
Object o = ois.readObject();
students = (Map<Integer,Student>)o;

System.out.println(students);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}
class Student implements Serializable{
Student(String name){
this.name = name;
}
private String name;

@Override
public String toString(){

return "Student name:"+name;
}
}
dracularking 2012-05-06
  • 打赏
  • 举报
回复
一般而言,读取次数超过写入次数时会发生EOFException,表明已到文件尾部,已无数据或对象可读。
对称地写读不会发生这类异常。
sdojqy1122 2012-05-06
  • 打赏
  • 举报
回复
求完整代码。

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧