//我的程序用于序列化和反序列化当前帐户的对象。它成功地读取和写入数据,但是当我再次重新运行程序时。最后写的对象是唯一出现的对象。有什么方法可以更改此代码,以便它写入每个对象并再次读回每个对象。我需要序列化整个arrayList,如果是这样,我该怎么做?这是我的代码。
package ie.lyit.bank;
import java.util.ArrayList;
import java.io.*;
import java.util.Scanner;
public class CurrentFileHandler implements Serializable
{
// Set up ArrayList to store currentAccount Objects
private ArrayList<Current> currentAcc;
// Set up default constructor
public CurrentFileHandler()
{
// Construct currentAcc ArrayList
currentAcc = new ArrayList<Current>();
}
/** An Add method that creates an account, reads details of an account
* and adds the account object to the ArrayList
**/
public void add()
{ // Create a currentAcc Object
Current current = new Current();
// Read its details
current.read();
// Add account to the ArrayList
currentAcc.add(current);
WriteRecordsToFile(current);
}
// A view method to allow a user to view an account
public Current view()
{
System.out.println("Enter the account number for the account you wish to view: ");
Scanner keyboardIn = new Scanner(System.in);
// Read the account number of the account to be viewed from the user
int accToView = keyboardIn.nextInt();
// for every account object in currentAcc
for(Current tmpCurrent: currentAcc)
{
// if it's number equals the number of accToView
if(tmpCurrent.getAccountNo() == accToView)
{
// Display it
System.out.println(tmpCurrent);
return tmpCurrent;
}
}
// Account not found, return null
System.out.println("Account Not Found.");
return null;
}
// A list method to allow a user to list the account(s)
public void list()
{
// Loop through the ArrayList
for(Current tmpCurrent: currentAcc)
// Display each account object
System.out.println(tmpCurrent);
}
// An Edit method to allow the user to view an account that they want to edit
public void edit()
{
// Call view() to find, display and return the account to be edited
Current tmpCurrent = view();
// if the account was !null, i.e. it was found
if(tmpCurrent != null)
{
// get it's index
int index = currentAcc.indexOf(tmpCurrent);
// Read in a new account and...
tmpCurrent.read();
// Reset it in the ArrayList
currentAcc.set(index, tmpCurrent);
}
}
// Delete method to allow the user to delete an account from the ArrayList
public void delete()
{
// Call view() to find, display and return the account to be deleted
Current tmpCurrent = view();
// if the account was !null, i.e. it was not found
if(tmpCurrent != null)
// Remove it from currentAcc ArrayList
currentAcc.remove(tmpCurrent);
}
// Serialization and Deserialization Section...
// A write record to file method
// Serializes the ArrayList and reads it out into the file
public void WriteRecordsToFile(Current c)
{
final String FILENAME = "current.bin";
try
{
// Serializes the ArrayList
FileOutputStream fileStream = new FileOutputStream(FILENAME);
ObjectOutputStream os = new ObjectOutputStream(fileStream);
System.out.println("Writing to file.... " + c);
os.writeObject(c);
os.close();
}
catch(FileNotFoundException fNFE)
{
System.out.println("Cannot create file to store currentAcc.");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
// A read record from file method
// Deserializes the ArrayList and reads it back into program
public void ReadRecordsFromFile()
{
final String FILENAME = "current.bin";
try
{
FileInputStream fis = new FileInputStream(FILENAME);
ObjectInputStream is = new ObjectInputStream(fis);
System.out.println("Reading from file...");
Current c;
c = (Current)is.readObject();
System.out.println(c);
is.close();
}catch(FileNotFoundException fNFE)
{
System.out.println("Cannot find current file.");
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}// end class