【发布时间】:2014-06-14 19:17:48
【问题描述】:
我正在尝试学习如何使用蒸汽,但我似乎无法弄清楚。 我正在尝试做的事情是将几个单独的对象添加到一个文件中,然后一次检索它们。 当我尝试检索它们时,我只会得到“最后一个”插入的对象。现在我正在尝试弄清楚如何打印对象,但后来我想将它们导入到 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");
}
}
【问题讨论】:
-
这里有什么问题
标签: java inputstream outputstream