我试图学习如何使用蒸汽,但我似乎无法弄明白。 我尝试做的事情是将几个单独的对象添加到文件中,然后一次性检索它们。 当我尝试检索它们时,我只得到了#34;最后一次"插入的对象。现在我试图找出如何打印对象,但后来我想将它们导入到ArrayList中。 这是我的代码:
public class ExpenseDB {
private final static File DB = new File("C:\\Expense2s3.dat");
public static void addExpense(Expense ex) throws AddException {
try {
ObjectOutputStream out;
out = new ObjectOutputStream(new FileOutputStream(DB));
out.writeObject(ex);
out.close();
System.out.println("Added "+ex);
} catch (IOException e) {
throw new AddException();
}
}
@SuppressWarnings("deprecation")
public static void getAllExpenses() {
if (DB.length() == 0) return;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(DB));
try {
Expense exp=(Expense)in.readObject();
System.out.println(exp);
in.close();
} catch (ClassNotFoundException e) {}
} catch (IOException e) {
System.err.println("Error");
}
}