title: Java - How to write ArrayList Objects into txt file?
body: <p>/** I have some methods likes add,display,sort,delete,and exit that implemented the ArrayList function. It works correctly, but the problem is that the objects that had been added were not saved on a .txt file, just the temporary objects. So I need to add them into text file,so that I can display and delete them later. Here's the part of the codes.
  */</p>
  
  <pre><code>public class testing {
  
      public static void main(String[] args) {
          String Command;
          int index = 0;
          Scanner input = new Scanner(System.in);
          ArrayList&lt;String&gt; MenuArray = new ArrayList&lt;String&gt;();
          boolean out = false;
          while (!out) {
              System.out.print("Enter your Command: ");
              Command = input.nextLine();
              // method ADD for adding object
              if (Command.startsWith("ADD ") || Command.startsWith("add ")) {
                  MenuArray.add(Command.substring(4).toLowerCase());
                  // indexing the object
                  index++;
                  /** i stuck here,it won't written into input.txt 
                  BufferedWriter writer = new BufferedWriter(new FileWriter(
                          "input.txt"));
                  try {
                      for (String save : MenuArray) {
                          int i = 0;
                          writer.write(++i + ". " + save.toString());
                          writer.write("\n");
                      }
                  } finally {
                      writer.close();
                  }*/
              } else if (Command.startsWith("EXIT") || Comand.startsWith("exit")) {
                  out = true;
              }
          }
      }
  }
  </code></pre>