title: Read and write an ArrayList to a .txt file
body: <p>I can't get this part of my project to work. (fileOut() and fileIn() methods). I would appreciate any help!</p>
  
  <p>I am trying to construct a method to open a text file and to write the details of an ArrayList of type  to it.</p>
  
  <p>I am also trying to construct a second method to open the text file containing details of all the bank’s accounts and uses the incoming data to create BankAccount objects using a BankAccount Constructor and storing each account in an ArrayList. </p>
  
  <p>Please note that other methods in the class need to use the information stored in the arraylist(s).
  Here is the main bit of code to focus on(reading and writing):</p>
  
  <pre><code>public void fileOut()
  {
      File fileName = new File("BankAccountFiles.txt");
      try{
          FileWriter fw = new FileWriter(fileName);
          Writer output = new BufferedWriter(fw);
          int numEntries = bankAccArrayList.size();
          for (int i = 0; i &lt; numEntries; i++) {
              output.write(bankAccArrayList.get(i).toString() + "\n"); 
          }
          output.close();
      }
      catch(Exception e) {
          JOptionPane.showMessageDialog(null,"File cannot be created");
      }
  }
  
  
  public void fileIn()
  {
      ArrayList&lt;BankAccount&gt; aList = new ArrayList&lt;BankAccount&gt;();
      String line;
      try {
          BufferedReader input = new BufferedReader(new FileReader("BankAccountFiles.txt"));
          if(!input.ready()) {
              throw new IOException();
          }
          while ((line = input.readLine()) != null) {
              aList.add(line);
          }
          input.close();
      } catch (IOException e) {
          JOptionPane.showMessageDialog(null,e);
      }
  }
  </code></pre>
  
  <p>Here is the code for the whole class:</p>
  
  <pre><code>import javax.swing.JOptionPane;
  import java.util.ArrayList;
  import java.io.*;
  import java.lang.*;
  import java.util.*;
  import javax.swing.*;
  
  public class MyBankController
  {
  
      private ArrayList&lt;BankAccount&gt; bankAccArrayList;
  
  
      public MyBankController() 
      {   
          bankAccArrayList = new ArrayList&lt;BankAccount&gt;();
      }
  
      public void createAccount (String accNum, String custName)
      {   
          bankAccArrayList.add(new BankAccount( accNum,custName));
          printAccountDetails(bankAccArrayList.get(bankAccArrayList.size()-1));
      }
  
      private void printAccountDetails(BankAccount incomingAcc)
      {   
          JOptionPane.showMessageDialog(null,incomingAcc.toString(),"Account Details",JOptionPane.PLAIN_MESSAGE);
      }
  
      public void listAllAccounts()
      {  
          String outputStr = "List of all accounts :\n\n";
  
          for(BankAccount account :  bankAccArrayList){
  
              outputStr += account + "\n\n";
          }
  
          JOptionPane.showMessageDialog(null,outputStr,"List of all Accounts",JOptionPane.PLAIN_MESSAGE);
      }
  
      public void listAllActiveAccounts()
      {  
          String outputStr = "List of all active accounts :\n\n";
          for(BankAccount account :  bankAccArrayList){
  
              if(account.getActive()) {
  
                  outputStr += account + "\n\n";
              }
          }
          JOptionPane.showMessageDialog(null,outputStr,"List of all Active Accounts",JOptionPane.PLAIN_MESSAGE);
      }
  
      private int getIndex(String bankAccNum)
  
      {   
          for (int i = 0; i &lt; bankAccArrayList.size(); i++) {
  
              if (bankAccNum.equals(bankAccArrayList.get(i).getAccNumber()))
              {
                  return i;
              }
          }
  
          JOptionPane.showMessageDialog(null,"Bank Account Number: " + bankAccNum + " is invalid","Error wrong account number",JOptionPane.ERROR_MESSAGE);
          return -1;
      }
  
      public void makeWithdrawal(String accNumber, double amount)
      {   
          if(getIndex(accNumber) != -1){ 
  
              bankAccArrayList.get(getIndex(accNumber)).makeWithdraw(amount);
          }
      }
  
      public void makeLodgement(String accNumber, double amount)
      {   
          if(getIndex(accNumber) != -1){ 
  
              bankAccArrayList.get(getIndex(accNumber)).makeLodgement(amount);
          }
      }
  
      public  void displayAccount(String accNumber)
      {   
          if(getIndex(accNumber) != -1){ 
  
              JOptionPane.showMessageDialog(null,(bankAccArrayList.get(getIndex(accNumber)).toString()),"Accounts Details",JOptionPane.PLAIN_MESSAGE);
          }
      }
  
      public  void closeAccount(String accNumber)
      {   
          if(getIndex(accNumber) != -1){ 
  
              bankAccArrayList.get(getIndex(accNumber)).closeAccount();
          }
      }
  
       public  void removeAccount(String accNumber)
      {   
  
          int index = getIndex(accNumber);
  
          if(index != -1){ 
  
              BankAccount myAcc = bankAccArrayList.get(index);
  
              if ((myAcc.getActive() == false) &amp;&amp; (myAcc.getAccBalance() == 0)){
  
                  bankAccArrayList.remove(index);
  
              }else if((myAcc.getActive() == false) &amp;&amp; (myAcc.getAccBalance() &gt; 0)){
  
                  int dialogResult = JOptionPane.showConfirmDialog(null," This account has a balance,do you wish to withdraw this balance " + 
                          "so as to remove the account ?"," Balance in inactive account",JOptionPane.PLAIN_MESSAGE);
  
                  if(dialogResult == JOptionPane.YES_OPTION){
  
                      myAcc.setActive();
                      myAcc.makeWithdraw(myAcc.getAccBalance());
                      myAcc.setActive();
                      bankAccArrayList.remove(index);
                      JOptionPane.showMessageDialog(null,"Account Removed","Confirmation",JOptionPane.PLAIN_MESSAGE);
                  }
  
              } else if((myAcc.getActive() == true) &amp;&amp; (myAcc.getAccBalance() == 0)){
  
                  int dialogResult = JOptionPane.showConfirmDialog(null," This account still has an active status, do you wish to change its status so as to remove account ?"  
                      ,"Account Active",JOptionPane.PLAIN_MESSAGE); 
  
                  if(dialogResult == JOptionPane.YES_OPTION){
  
                      myAcc.setActive();
                      bankAccArrayList.remove(index);                              
                      JOptionPane.showMessageDialog(null,"Account Removed","Confirmation",JOptionPane.PLAIN_MESSAGE);
  
                  }
  
              } else{
  
                  int dialogResult = JOptionPane.showConfirmDialog(null," This account still has an active status and a balance, do you wish to close the account so as remove it" 
                      ,"Account Active with Balance",JOptionPane.PLAIN_MESSAGE);
  
                  if(dialogResult == JOptionPane.YES_OPTION){
  
                      myAcc.closeAccount();
                      bankAccArrayList.remove(index);                                
                      JOptionPane.showMessageDialog(null,"Account Removed","Confirmation",JOptionPane.PLAIN_MESSAGE);
                  }                
              }
          }
      }
  
  
          public void customerInterface() 
      {
          String accNumber = JOptionPane.showInputDialog(null,"Please enter your account number","Account Login",JOptionPane.PLAIN_MESSAGE);
          if(getIndex(accNumber) != -1)
          {
               String numOption = JOptionPane.showInputDialog(null,"Please select an option below:\n\n" + 
              " [1] Make a lodgment:\n\n [2] Make a withdrawal:\n\n [3] Display account details:\n\n" +
              " [4] Close account:\n\n [5] Exit","MyBank ATM",JOptionPane.PLAIN_MESSAGE);
              if (numOption == null) //User presses cancel or 'x'.
              {
                  JOptionPane.showMessageDialog(null,"Goodbye.","MyBank System",JOptionPane.INFORMATION_MESSAGE);
              }
              else if(Integer.parseInt(numOption) == 1) //Converts numOption from String to Integer.
              {
                  //Brings up lodgement interface
                  String amount = JOptionPane.showInputDialog("Please enter the amount you would like to lodge.");
                  makeLodgement(accNumber, Double.parseDouble(amount)); // Lodges amount into account.
                  customerInterface(); 
              }
              else if(Integer.parseInt(numOption) == 2)
              {
               String amount = JOptionPane.showInputDialog("Please enter the amount you wish to withdraw.");
               makeWithdrawal(accNumber, Double.parseDouble(amount)); //Call on makeWithdrawl method.
               customerInterface();
              }
              else if(Integer.parseInt(numOption) == 3)
              {
                  displayAccount(accNumber); //Calls on displayAccount method.
                  customerInterface();
              }
              else if(Integer.parseInt(numOption) == 4)
              {
                  closeAccount(accNumber); //Call on close account method.
                  customerInterface();
              }
              else if(Integer.parseInt(numOption) == 5)
              {
                  return; //Exits system.
              }
              else if(Integer.parseInt(numOption) &gt; 5 || Integer.parseInt(numOption) &lt; 1) //If number enter is outside of 1-5.
              {
                JOptionPane.showMessageDialog(null,"Please enter a number between 1-5 and try again.","Error",JOptionPane.ERROR_MESSAGE);
                customerInterface();
              }   
          }
      }
  
  
      public void bankInterface()
      {
              String numOption = JOptionPane.showInputDialog(null,"Please select an option below:\n\n" + 
              " [1] Display All Accounts:\n\n [2] Display All Active Accounts:\n\n [3] Open a New Account:\n\n" +
              " [4] Close an Existing Account:\n\n [5] Run Start of Day:\n\n [6] Run End of Day:\n\n [7] Exit:","MyBank System",JOptionPane.PLAIN_MESSAGE);
              if (numOption == null)
              {
                  JOptionPane.showMessageDialog(null,"Goodbye.","MyBank System",JOptionPane.INFORMATION_MESSAGE); //User presses cancel or 'x'.
              }
              else if(Integer.parseInt(numOption) == 1) //Converts numOption from String to Integer.
              {
                  //Displays all accounts.
                  listAllAccounts();
                  bankInterface();
              }
              else if(Integer.parseInt(numOption) == 2)
              {
                  //Display all active accounts.
                  listAllActiveAccounts();
                  bankInterface();
              }
              else if(Integer.parseInt(numOption) == 3)
              {
                  //Open a new account
                  String accNum = JOptionPane.showInputDialog("Please allocate an account number:");
                  String custName = JOptionPane.showInputDialog("Please enter the customers name:");
                  createAccount (accNum, custName);
                  bankInterface();
              }
              else if(Integer.parseInt(numOption) == 4)
              {
                  //Close an existing account.
                  String accNumber = JOptionPane.showInputDialog("Please enter the account number you would like to close:");
                  closeAccount(accNumber);
                  bankInterface();
              }
              else if(Integer.parseInt(numOption) == 5)
              {
                  //Run start of day file.
                  fileIn();
                  bankInterface();
              }
              else if(Integer.parseInt(numOption) == 6)
              {
                  //Run end of day file.
                  fileOut();
                  bankInterface();
              }
              else if(Integer.parseInt(numOption) == 7)
              {
                  //Exits system.
                  return;
              }
              else if(Integer.parseInt(numOption) &gt; 7 ||Integer.parseInt(numOption) &lt; 1) //If user enters number outside of 1-7.
              {
                  JOptionPane.showMessageDialog(null,"Please enter a number between 1-7 and try again.","Error",JOptionPane.ERROR_MESSAGE);
                  bankInterface();
              }
      }
  
  
      public void fileOut()
      {
          File fileName = new File("BankAccountFiles.txt");
          try{
              FileWriter fw = new FileWriter(fileName);
              Writer output = new BufferedWriter(fw);
              int numEntries = bankAccArrayList.size();
              for (int i = 0; i &lt; numEntries; i++) {
                  output.write(bankAccArrayList.get(i).toString() + "\n"); 
              }
              output.close();
          }
          catch(Exception e) {
              JOptionPane.showMessageDialog(null,"File cannot be created");
          }
      }
  
  
      public void fileIn()
      {
          ArrayList&lt;BankAccount&gt; aList = new ArrayList&lt;BankAccount&gt;();
          String line;
          try {
              BufferedReader input = new BufferedReader(new FileReader("BankAccountFiles.txt"));
              if(!input.ready()) {
                  throw new IOException();
              }
              while ((line = input.readLine()) != null) {
                  aList.add(line);
              }
              input.close();
          } catch (IOException e) {
              JOptionPane.showMessageDialog(null,e);
          }
      }
  }
  </code></pre>