在文件file(student.dat)写入oos.writeObject(new HashSet<Student>).
Student类有姓名,年龄,生日(生日要求不是用String的年月日)属性。
文件中只保存一个HashSet对象,在hasSet中保存几个学生,
如姓名:yang,zhu,zhang,.要求删除姓名为yang的学生全部信息。我不知道删除,请大哥们帮帮忙。谁做对给多分哦或全部分。

解决方案 »

  1.   

    你可以从文件中读出来,还原成HashSet,然后从set中删除Studet后,再存回原文件。
      

  2.   


           // file   前面声明了。
    public boolean removeAgent(String name)
    {
    FileInputStream fis = null;
    ObjectInputStream ois = null;

    OutputStream out = null;
    ObjectOutputStream oos = null;
    HashSet<Agent> set = null;
    boolean boolRemove = false; try
    {
    fis = new FileInputStream(file);
    ois = new ObjectInputStream(fis);
    set = (HashSet<Agent>) ois.readObject();

    Iterator<Agent> it = set.iterator();
    while(it.hasNext())
    {
    Agent agent = (Agent) it.next();
    System.out.println(agent);
    if(agent.getName().equals(name))
    {

    set.remove(agent);

    return true;
    }
    } }
    catch (FileNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally
    {
    if (ois != null)
    {
    try
    {
    ois.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if (fis != null)
    {
    try
    {
    fis.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    try
    {
    out = new FileOutputStream(file);
    oos = new ObjectOutputStream(out);

    oos.writeObject(set);
    boolRemove = true;
    if(boolRemove == true)
    {
    JOptionPane.showMessageDialog(new JFrame(), "修改成功!");
    return boolRemove;

    }


    }
    catch (FileNotFoundException e)
    {

    e.printStackTrace();
    }
    catch (IOException e)
    {

    e.printStackTrace();
    }
    finally
    {
    if (oos != null)
    {
    try
    {
    oos.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if (out != null)
    {
    try
    {
    out.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    return false;
    }
      

  3.   


    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;public class StudentWriter { private static String file="d:/student.dat";
    public static void main(String[] args) throws Exception{
    Set<Student> studentSet=new HashSet<Student>();
    studentSet.add(new Student("first",1983,04,23));
    studentSet.add(new Student("second",1984,05,26));
    studentSet.add(new Student("third",1982,01,29));
    System.out.println("write three students to file:\n"+studentSet);
    writeStudent(studentSet);
    studentSet=readStudent();
    System.out.println("read three students from file:\n"+studentSet);
    studentSet.remove(new Student("second",1984,05,26));
    }

    private static void writeStudent(Set ss) throws Exception{
    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
    oos.writeObject(ss);
    oos.close();
    }
    private static Set readStudent() throws Exception{
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
    return (Set)ois.readObject();

    }
    }
    class Student implements Serializable{
    private String name="";
    private int age;
    private Date birthday=new Date();

    public Student(String name, int year, int month, int day){
    this.name=name;
    Calendar c=Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH,month);
    c.set(Calendar.DAY_OF_MONTH,day);
    setBirthday(c.getTime());

    }
    public int getAge() {
    return age;
    }

    public Date getBirthday() {
    return birthday;
    }
    public void setBirthday(Date birthday) {
    this.birthday = birthday;
    age=new Date().getYear()-birthday.getYear();
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    @Override
    public boolean equals(Object arg0) {
    if (arg0 instanceof Student){
    Student other=(Student)arg0;
    return this.name.equals(other.name);
    }else{
    return false;
    }
    }
    @Override
    public int hashCode() {
    return name.hashCode();
    }


    public String toString(){
    return "Name:"+name+",age:"+age;
    }

    }
      

  4.   


    FileInputStream fis = null;
    ObjectInputStream ois = null;

    OutputStream out = null;
    ObjectOutputStream oos = null;
    HashSet<Agent> set = null;
    boolean boolRemove = false; try
    {
    fis = new FileInputStream(file);
    ois = new ObjectInputStream(fis);
    set = (HashSet<Agent>) ois.readObject();
    //在这里改,Agent 必须定义一个Agent(String name)构造方法。
    boolRemove = set.remove(new Agent(name)); }
    catch (FileNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    finally
    {
    if (ois != null)
    {
    try
    {
    ois.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if (fis != null)
    {
    try
    {
    fis.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    try
    {
    out = new FileOutputStream(file);
    oos = new ObjectOutputStream(out);

    oos.writeObject(set);
                           //这里也改一点
    if(boolRemove == true)
    {
    JOptionPane.showMessageDialog(new JFrame(), "修改成功!");
    return boolRemove;

    }else
    {
    JOptionPane.showMessageDialog(new JFrame(), "操作失败!");
    return boolRemove;
    }


    }
    catch (FileNotFoundException e)
    {

    e.printStackTrace();
    }
    catch (IOException e)
    {

    e.printStackTrace();
    }
    finally
    {
    if (oos != null)
    {
    try
    {
    oos.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    if (out != null)
    {
    try
    {
    out.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    return boolRemove;
    }