高分求高手解决java程序设计问题: 将几个对象写入TXT文件之后的查找与输出

我建立了一个类。用此类创建了5个对象。现在将这5个对象写入到一个TXT文件中。然后想通过查找对象的一个属性来找到符合要求的对象,并显示到屏幕上。这个怎么做啊?求高手给出思路&&详细代码。
解决了会追加分数的。谢谢了!
有不清楚的地方可以直接百度HI我

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjTest {
ObjectOutputStream oos;
ObjectInputStream ois;
String url = "f:/a.txt";//txt文件的目录位置,请自行修改

{
try {
oos =new ObjectOutputStream(new FileOutputStream(url));
ois = new ObjectInputStream(new FileInputStream(url));
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭流
public void close(){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//写入txt的方法
public void writeOut(Object obj){
try {

oos.writeObject(obj);
oos.flush();
} catch (Exception e) {
e.printStackTrace();
}
}

//读取txt的方法
public Object readIn(){
try {

return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void main(String[] args) throws Exception {
ObjTest ot = new ObjTest();
ot.writeOut(new Person(1,"Tom",true));//写入5个对象
ot.writeOut(new Person(2,"Mary",false));
ot.writeOut(new Person(3,"Jack",true));
ot.writeOut(new Person(4,"Rose",false));
ot.writeOut(new Person(5,"Peter",true));

Person p;
while((p = (Person)ot.readIn()) != null){
if(p.id == 3){ //查找条件
System.out.println(p);
return;
}
}
System.out.println("no found person");
ot.close();
}

}

class Person implements Serializable{
int id;
String name;
boolean sex;
public Person(){}
public Person(int id, String name, boolean sex) {
super();
this.id = id;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return id+ "\r\n" + name + "\r\n" + sex +"\r\n";
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-12-03
读取内容是I/O 的知识。InputStream读取,OutputStream输出显示.
思路:比如每个对象有a,b,c,d 四个属性或者方法。通过查找c属性来找到符合要求的对象,直接查找c就行了。
第2个回答  2011-12-03
我对你的意思有点模糊 你现在在么 在hi我追问

我在。你HI我就可以了。我看不到你的用户名

第3个回答  2011-12-03
序列化。追问

具体怎么使用呢?