2016-12-05 61 views
-2

我将代码配对到我遇到问题的方法。这似乎是工作,直到我再次加载文件,它没有任何内容。 (我还没有完全理解如何在执行第二次加载之前清除ArrayList,但这是以后的)。java howto加载并保存ArrayList对象

对不起,如果这是隐藏在某个其他术语下的某处我还没有学到,但这是一个明天到期的项目,我在我的智慧结束。

import java.util.*; 
import java.io.*; 

public class MainATM3 { 

    public static ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>(); 
    public static ClientAccount editBankAccount = new ClientAccount("placeholder",1234,1);; 

    public static void main(String[] args) { 

// Create ATM account ArrayList 
    ArrayList<ClientAccount> accounts = new ArrayList<ClientAccount>(); 
// Get Account data from files 
initialLoadATMAccounts(accounts); 
System.out.println("Loaded "+accounts.size()); 

System.out.println("before Array "+(accounts.size())); 
    accounts.add(0,new ClientAccount("Jess",500,1830)); 
    accounts.add(1,new ClientAccount("Mary",1111.11,7890)); 
System.out.println("after Array "+(accounts.size())); 

saveATMAccounts(accounts); 
System.out.println("saved "+(accounts.size())); 

initialLoadATMAccounts(accounts); 
System.out.println("Loaded "+accounts.size()); 

    System.out.println("Logged Out"); 

    }  

// Save ArrayList of ATM Objects //call by: saveATMAccounts(accounts); 
    public static void saveATMAccounts(ArrayList<ClientAccount> saveAccounts) { 
    FileOutputStream fout = null; 
    ObjectOutputStream oos = null; 
    try{ 
     fout=new FileOutputStream("ATMAccounts.sav"); 
     oos = new ObjectOutputStream(fout); 
     oos.writeObject(accounts); 
     System.out.println("objects written "+(accounts.size())); 
     } catch (Exception ex) { 
     ex.printStackTrace(); 
     } finally { 
     if (fout != null) { 
      try { 
      fout.close(); 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
      } 
     if (oos != null) { 
      try { 
      oos.close(); 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
     } 
    } 
    } 


    // INITIAL Load ArrayList of ATM Objects //call by: initialLoadATMAccounts(accounts); 
    public static void initialLoadATMAccounts(ArrayList<ClientAccount> loadAccounts){ 
    FileInputStream fIS = null; 
    ObjectInputStream oIS = null; 
    try{ 
     fIS=new FileInputStream("ATMAccounts.sav"); 
     oIS = new ObjectInputStream(fIS); 
     ArrayList<ClientAccount> loadAccounts = (ArrayList<ClientAccount>) oIS.readObject(); 
     oIS.close(); 
     fIS.close(); 
    } 
    catch(Exception exc){ 
     exc.printStackTrace(); 
    } 
    } 
} 


import java.io.Serializable; 
public class ClientAccount implements Serializable { 

    public String accountName; 
    public double accountBalance; 
    public int accountPIN; 

    public ClientAccount(String accountName, double accountBalance, int accountPIN){ 
     this.accountName=accountName; 
     this.accountBalance=accountBalance; 
     this.accountPIN=accountPIN; 
    } 

// Account Name Methods  
    public String getAccountName() { 
     return accountName; 
    } 
    public void setAccountName(String name) { 
     accountName = name; 
    } 

// Account Balance Methods 
    public double getAccountBalance() { 
     return accountBalance; 
    } 
    public void setAccountBalance(double balance) { 
     accountBalance = balance; 
    } 

// PIN Methods 
    public int getAccountPIN() { 
     return accountPIN; 
    } 
    public void setAccountPIN(int newPIN) { 
     accountPIN = newPIN; 
    } 

} 
+0

我只是初始加载。你从哪里重新加载? – MordechayS

+0

你想在'initialLoadATMAccounts(ArrayList loadAccounts)'中做什么?你为什么不在你的方法的任何地方使用'loadAccounts'参数?如果你不使用它,那么为什么你的方法有这个参数?有些东西告诉我,你可能需要阅读[Java是“通过引用传递”还是“传递值”?](http://stackoverflow.com/questions/40480/is-java-pass-by-基准或通按值)。 – Pshemo

+0

您应该使用[try-with-resources](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)。另外,你可能希望你的加载方法返回一个'ArrayList'。 – 4castle

回答

0

不是传递所需的阵列initialLoadATMAccounts为PARAM你应该返回新的,装阵:

public static List<ClientAccount> initialLoadATMAccounts(){ 
    FileInputStream fIS = null; 
    ObjectInputStream oIS = null; 
    try{ 
     fIS=new FileInputStream("ATMAccounts.sav"); 
     oIS = new ObjectInputStream(fIS); 
     ArrayList<ClientAccount> loadAccounts = (ArrayList<ClientAccount>)  oIS.readObject(); 
     oIS.close(); 
     fIS.close(); 
     return loadAccounts; 
    } 
    catch(Exception exc){ 
     exc.printStackTrace(); 
    } 
} 

BTW:像Eclipse的一个IDE会发出警告,在那里你覆盖帕拉姆loadAccounts 。