在文件file(student.dat)写入oos.writeObject(new HashSet<Student>).
Student类有姓名,年龄,生日(生日要求不是用String的年月日)属性。
文件中只保存一个HashSet对象,在hasSet中保存几个学生,
如姓名:yang,zhu,zhang,.要求删除姓名为yang的学生全部信息。我不知道删除,请大哥们帮帮忙。谁做对给多分哦或全部分。
Student类有姓名,年龄,生日(生日要求不是用String的年月日)属性。
文件中只保存一个HashSet对象,在hasSet中保存几个学生,
如姓名:yang,zhu,zhang,.要求删除姓名为yang的学生全部信息。我不知道删除,请大哥们帮帮忙。谁做对给多分哦或全部分。
解决方案 »
- 谁能告诉我一个DJ java decompiler的下载地址?
- Java中import的内容多还是少对执行效率有影响吗?
- 我是一个专科毕业的女孩,明天就要去面试了,麻烦大哥大姐指点一下,我第一次面试!
- linux下写文件乱码,求救阿,救命了
- 请教JSF里复选框的选定问题
- java.net.URL/java.io.InputStream的怪问题(新手)
- 各位大虾:救命了(50,小弟穷哈)
- 请教一个Java小问题,希望大家多帮忙!
- 还是replaceFirst的问题
- 失误,一不小心分没加上,请回我的贴字的人来拿分,名单在内(我一次只能给100分)scend
- Swing和Swt那个跟具有长远的发展前景
- jpanel里怎样放置100*100的格子问题
// 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;
}
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;
}
}
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;
}