title: How to write code data into a text file
body: <p>I just did a simple code which takes user name and phone number and save those into an arraylist by creating object. I want to save those information (name and phonenumber) into a text file so that all old information I can get again. How do I do it? Here is my code ... </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Scanner;
  public class manager {
  
      Scanner input = new Scanner(System.in);
      ArrayList &lt;objectclass&gt; Test = new ArrayList &lt;objectclass&gt; ();
  
  
          public void mainloop() {
          for (int i = 0; i &lt; 100; i++) {
  
              String x;
  
              System.out.println("Please Select your option");
              System.out.println("............................");
              System.out.println("1 ADD NAME AND NUMBER\n2 SEARCH NAME AND NUMBER \n0 EXIT");
              System.out.println("............................");
  
              x = input.nextLine();
  
              if (x.equalsIgnoreCase("0")) {
                  System.out.println("Thank you!");
                  break;
              }
              if (x.equalsIgnoreCase("1")) {
                  String Name;
                  String Number;
  
                  System.out.println("Please Enter your Name below");
                  Name = input.nextLine();
                  System.out.println("Please Enter your Number below");
                  Number = input.nextLine();
  
                  objectclass objectclassObject = new objectclass(Name, Number);
                  Test.add(objectclassObject);
              }
              if (x.equalsIgnoreCase("2")) {
                  String y;
                  System.out.println("*** Enter your Name below for search ***");
                  y = input.nextLine();
  
                  for (objectclass p : Test) {
                      String z = p.getName();
                      if (z.equalsIgnoreCase(y)) {
                          System.out.println("Your Name is: " + p.getName() + "\nYour Number is: " + p.getNumber());
                          System.out.println("");
                      } else {
                          System.out.println("Contact not Found!\n");
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I want to save all name and number that I store in arraylist into a text file ... how can I do it? </p>
  
  <p>I tried this so far but don't know what to do next ... 
      import java.io.<em>;
      import java.lang.</em>;
      import java.util.*;</p>
  
  <pre><code>public class creatfile {
  
  private Formatter x;
  
  public void openFile(){
      try{
          x = new Formatter("testkyo");
      }catch (Exception e){
          System.out.println("you have an error");
      }
  }
  public void addRecord(){
      x.format();
  }
  public void closeFile(){
      x.close();
  }
  </code></pre>