如何在包含名称和年龄的对象的文件中按名称查找?
我想从文件中按名称搜索它。我完成了将对象写入文件并从文件中读取的操作。
Demo object1 = new Demo(100,"saran");
Demo object2 = new Demo(20 ,"nivas");
ArrayList<Demo> list=new ArrayList<>();
try {
FileOutputStream file=new FileOutputStream("C:\\Users\\SARANNIVAS\\Documents\\file\\obj");
ObjectOutputStream out=new ObjectOutputStream(file);
list.add(object1);
list.add(object2);
out.writeObject(list);
out.close();
file.close();
System.out.println("object has been serialized");
} catch(IOException ex)
{
ex.printStackTrace();
System.out.println("exception is caught");
}
try {
FileInputStream file=new FileInputStream("C:\\Users\\SARANNIVAS\\Documents\\file\\obj");
ObjectInputStream in=new ObjectInputStream(file);
ArrayList<Demo> list1=new ArrayList<>();
list1=(ArrayList<Demo>)in.readObject();
for(Demo d:list1) {
System.out.println(d.a);
System.out.println(d.b);
}
} catch(IOException ex) {
System.out.println("IOException is caught");
}
皈依舞
相关分类