在java中序列化ArrayList

时间:2015-02-13 03:46:23

标签: java serialization arraylist deserialization

这是我第一次使用序列化。我在arraylist中有我的数据。当我反序列化时,arraylist是空的,我无法弄清楚原因。 arraylist只包含两个对象(但将来可以容纳更多)的Account类。 Account类是可序列化的,因为我相信ArrayList对象的情况。这是我的代码:

public class BankDatabase implements Serializable
{
   public ArrayList<Account> accounts;
   private static ObjectOutputStream output;
   private static ObjectInputStream input;

   public BankDatabase()
   {
      accounts = new ArrayList();
      File database = new File("database.ser");
      if(!database.exists())
  {          
          Account testAccount[] = new Account[2];
          testAccount[0] = new Account(12345, 54321, 1000.0, 1200.0);
          testAccount[1] = new Account(98765, 56789, 200.0, 200.0);  
          accounts.add(0, testAccount[0]);
          accounts.add(1, testAccount[1]);
      }
      else
          loadDatabase();
   } 

   public static void openIn()
   {
       try
       {   
           input = new ObjectInputStream(Files.newInputStream(Paths.get("database.ser")));
       }
       catch (IOException ioException)
       {
           System.err.println("Could not open file.  Closing Program.");
           System.exit(1);
       }
   }

   public static void openOut()
   {
       try
       {   
           output = new ObjectOutputStream(Files.newOutputStream(Paths.get("database.ser")));
       }
       catch (IOException ioException)
       {
           System.err.println("Could not open file.  Closing Program.");
           System.exit(1);
       }
   }

   public void readData()
   {
       try
       {       
           while(true)
           {
               accounts.add((Account) input.readObject());
           }
       }
       catch (EOFException endOfFileException)
       {
           System.out.printf("No More Records%n");
       }
       catch (ClassNotFoundException classNotFoundException)
       {
           System.err.println("Invalid object type.");
       }
       catch (IOException ioException)
       {
           System.err.println("Could not read file.");
       }
   }

   public void saveData()
   {       
       try
       {
           for(Account currentAccount : accounts)               
           {
               output.writeObject(currentAccount);
           }
       }
       catch (IOException ioException)
       {
           System.err.println("Error writing to file.  Terminating");
       }
   }

   public void closeOut()
   {
       try
       {
           if (output != null)
               output.close();
       }
       catch (IOException ioException)
       {
           System.err.println("Error closing file.  Terminating.");
           System.exit(1);
       }
   }

   public void closeIn()
   {
       try
       {
           if (input != null)
               input.close();
       }
       catch (IOException ioException)
       {
           System.err.println("Error closing file.  Terminating.");
           System.exit(1);
       }
   }

   public void loadDatabase()
   {
       openIn();
       readData();
       closeIn();
   }

   public void updateDatabase()
   {
       openOut();
       saveData();
       closeOut();
   }
} 

public class Account implements Serializable 
{
   public int accountNumber; 
   public int pin; 
   public double availableBalance; 
   public double totalBalance; 

   public Account(int theAccountNumber, int thePIN, 
      double theAvailableBalance, double theTotalBalance)
   {
      accountNumber = theAccountNumber;
      pin = thePIN;
      availableBalance = theAvailableBalance;
      totalBalance = theTotalBalance;
   }

1 个答案:

答案 0 :(得分:0)

ArrayList是Serializable,执行objectOuput.writeObject(list)保存,List list = (List)objectInputStream.readObject()回读