title: FileWriter only displays the last line
body: <p>When I display the variable data using <code>System.out.println(data)</code>, it displays the content (all lines) of the &quot;filename.txt&quot;.
  However, when I use <code>myWriter.write(data)</code>, it only writes the last line of the initial file.
  My task is to read a file (in this case, <code>filename.txt</code>) and copy its content into a new file (<code>new.txt</code>).</p>
  <pre class="lang-java prettyprint-override"><code>package javaapplication13;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.util.Scanner;
  import java.io.PrintWriter;
  import java.io.FileWriter;
  import java.io.IOException;
  
  
  public class readFile {
      public static String data;
       
      public static void main(String[] args) throws IOException{
          try{
              File myObj = new File(&quot;C:\\Users\\Admin\\Documents\\NetBeansProjects\\JavaApplication13\\src\\javaapplication13\\filename.txt&quot;);
              try (Scanner myReader = new Scanner(myObj)) { 
                  do{ 
                       data = myReader.nextLine();   
                  }
                  while (myReader.hasNextLine());
   
                    PrintWriter out = new PrintWriter(&quot;C:\\Users\\Admin\\Documents\\NetBeansProjects\\JavaApplication13\\src\\javaapplication13\\new.txt&quot;);
                    FileWriter myWriter = new FileWriter(&quot;C:\\Users\\Admin\\Documents\\NetBeansProjects\\JavaApplication13\\src\\javaapplication13\\new.txt&quot;);
                    myWriter.write(data);
                     
                    myWriter.close();
                    out.close();
                    myReader.close();
    
              } 
          } 
          catch (FileNotFoundException e){
              System.out.println(&quot;An error occurred.&quot;);
          }
      }
  }
  </code></pre>