我有一个项目,我需要阅读和写一个银行帐户对象的矢量,我正在努力解决这个问题。当帐户没有存储在向量中时,我能够弄明白,但现在我正在处理向量,我很困惑。如果有人可以查看我的代码,我将非常感谢!提前感谢您的时间!
这是我最好的尝试。即使我知道这是错的,我只是想让你知道我想要做什么。
public static void readTrans()
{
textArea.setText("");
chooseFile(1);
try
{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
for (int index=0; index != fileIndex; index++)
{
account = (CheckingAccount)in.readObject();
acctStore.add(index, account);
System.out.println("reading account " + acctStore.elementAt(index));
}
saveStatus = true;
in.close();
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch (IOException e)
{
System.out.println(e);
}
}
public static void writeTrans()
{
textArea.setText("");
chooseFile(2);
try
{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fos);
for (int index=0; index != acctStore.size(); index++)
{
out.writeObject(acctStore.elementAt(index));
System.out.println("Writing account for " + acctStore.elementAt(index).getName() +" with initial balance: " + acctStore.elementAt(index).getBalance());
fileIndex++;
}
saveStatus = true;
out.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
好的我觉得我弄清楚了,但是因为我把矢量放到了CheckingAccount上,所以我收到了警告。这种做法好吗?该程序按我的预期工作,所以我假设如此。再次感谢您的时间!
这是更新后的代码:
public static void readTrans()
{
textArea.setText("");
chooseFile(1);
try
{
FileInputStream fis = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(fis);
acctStore = (Vector<CheckingAccount>)in.readObject();
saveStatus = true;
in.close();
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch (IOException e)
{
System.out.println(e);
}
}
public static void writeTrans()
{
textArea.setText("");
chooseFile(2);
try
{
FileOutputStream fos = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(acctStore);
saveStatus = true;
out.close();
}
catch(IOException e)
{
System.out.println(e);
}
}