title: Write multiple lines in a text file ( java)
body: <p>This following code is to run a command in <code>cmd</code> and to produce a text file with the output from the command line. The code below shows the correct information in the output window in Eclipse, but only one last line is printed in the text file. Can anyone help me with this?</p>
  
  <pre><code>import java.io.*;
  
  public class TextFile {
  public static void main(String[] args) throws IOException {
      try {
          Process p = Runtime.getRuntime().exec("git log");
  
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(p.getInputStream()));
          BufferedWriter writer = null;
  
          String line = null;
          while ((line = in.readLine()) != null) {
  
              writer = new BufferedWriter(new FileWriter("textfile.txt"));
              writer.write(line);
              System.out.println(line);
  
              writer.close();
          }
  
      } catch (IOException e) {
          e.printStackTrace();
  
      }
  
     }
  }
  </code></pre>