java吧 关注:1,308,202贴子:12,882,712
  • 0回复贴,共1

急急急!java新手求教!!关于从文件中读取hashmap的问题

只看楼主收藏回复

用java做了一个学生信息管理系统,先在输入类输入信息写入hashmap再存到一个文件里,再在查询类读取文件中的hashmap,但是读取会显示hashmap为空,这是怎么回事呢??和在不同类有关吗?
输入部分:
try
{
FileOutputStream fout=new FileOutputStream("学生信息.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
学生信息表.put(学号, student);
out.writeObject(学生信息表);
fout.close();
out.close();
}
catch (Exception ee)
{
}
查找部分:
try
{
FileInputStream fin=new FileInputStream("学生信息.txt");
ObjectInputStream in=new ObjectInputStream (fin);
学生信息表= (HashMap<String, Student>) in.readObject();
fin.close();
in.close();
}
catch (Exception ee)
{
}
if (学生信息表 != null)
{
Iterator keys = 学生信息表.keySet().iterator();
while (keys.hasNext())
{
String key = (String) keys.next();
if (学号.equals(key))
{
Student s = (Student) (学生信息表.get(学号));
name.setText(s.getName());
major.setText(s.getMajor());
grade.setText(s.getGrade());
birthday.setText(s.getBirthday());
if (s.getGender() == "男")
{
j1.isSelected();
}
else
{
j2.isSelected();
}
}
else
{
JOptionPane.showMessageDialog(this, "未找到该生信息!",
"警告", JOptionPane.INFORMATION_MESSAGE);
}
}
}
else{JOptionPane.showMessageDialog(this, "哈希表为空!",
"警告", JOptionPane.INFORMATION_MESSAGE);}
}}}}
快崩溃了,请各位大大帮帮我


IP属地:北京1楼2017-08-11 17:30回复