title: How do I add data to text file and not overwrite what I have in Java
body: <p>This is my code so far but it overwrites what I have in the text file already. What I want is for it to add it to a new line in the text file.</p>
  
  <pre><code>import java.io.*;
  import java.util.Scanner;
  
  public class Login{
    public static void main(String[] args) throws IOException {
      Scanner s1,s2;
      s1 = new Scanner(new FileInputStream("login.txt"));
      s2 = new Scanner(System.in);
      boolean loggedIn = false;
      String name,pword,n,p;
      System.out.println("Are you a new user? (Type y for yes or n for no)");
      String nU = s2.next();
  
      if (nU.equals("n"))
      {
      System.out.println("Enter username:");
      n=s2.next();
      System.out.println("Enter password:");
      p=s2.next();
      while(s1.hasNext()){
        name=s1.next();
        pword=s1.next();
        if(n.equals(name) &amp;&amp; p.equals(pword)){
          System.out.println("You are logged in.");
          loggedIn = true;
          break;
        }
      }
      if(!loggedIn)
        System.out.println("Incorrect password or username.");
      }
      else if (nU.equals("y"))
      {
  </code></pre>
  
  <p>Down here is where the problem with my code will be as this is where it is writing it to the file.</p>
  
  <pre><code>     PrintWriter out = new PrintWriter("login.txt"); 
       System.out.println("Enter username:");
       n=s2.next();
       System.out.println("Enter password:");
       p=s2.next();
       out.append(n);
       out.append(p);
       out.close();
       System.out.println("Account has been created and you are logged in.");
      }
      else
        System.out.println("Invalid response.");
  </code></pre>