有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中序列化ArrayList

这是我第一次体验序列化。我的数据在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) 个答案

  1. # 1 楼答案

    ArrayList是可序列化的,请执行objectOuput.writeObject(list)保存,执行List list = (List)objectInputStream.readObject()回读