【发布时间】:2015-07-09 01:10:42
【问题描述】:
在我的程序中,用户提供了一些输入,这些输入被添加到 arrayList 中。 arrayList 对象被写入文件。因此,我创建了一个用于写入和读取文件的方法(使用 file-i/o 和 object-i/o 流)。
现在如果我想从文件中删除特定对象,我该怎么做?
这是我正在处理的部分:
case 4:
bankAccounts2=(List<BankAccount>)rw.readFromFile();//method calling; BankAccount is the class for settre and getter
Iterator<BankAccount> it=bankAccounts2.iterator();//bankAccounts2 is the arrayList for reading file
System.out.println("Enter the account you want to delete:");
deleteAcc = sc.nextInt();
while (it.hasNext()) {
BankAccount next = it.next();
if (next.getAccNum() == deleteAcc) {
it.remove();
}
}
break;
【问题讨论】:
-
重写没有内容的文件?
-
“[如何] 在文件中查找一行并将其删除”stackoverflow.com/questions/1377279/…
标签: java list file-io arraylist fileoutputstream