title: Files.write() is not working properly, while BufferedWriter seems to be fine
body: <p>I had a simple exercise to do:</p>
  
  <ul>
  <li>read a file</li>
  <li>filter out all lines beginning with "//"</li>
  <li>remove additional spaces</li>
  <li>write it back to a file</li>
  </ul>
  
  <p>And I came up with following code:</p>
  
  <pre><code>import java.io.BufferedWriter;
  import java.io.IOException;
  import java.nio.file.StandardOpenOption;
  import java.util.List;
  import java.util.stream.Collectors;
  
  import static java.nio.file.Files.lines;
  import static java.nio.file.Files.newBufferedWriter;
  import static java.nio.file.Files.write;
  import static java.nio.file.Paths.get;
  import static java.util.stream.Collectors.toList;
  
  public class FileParser {
      public static final String PATH = "test.txt";
  
      public static void main(String... args) {
          try {
              List&lt;String&gt; strings = lines(get(PATH)).filter(line -&gt; !line.startsWith("//")).map(line -&gt; line.trim().replaceAll(" +", " ")).collect(toList());
  //            write(get(PATH), strings, StandardOpenOption.WRITE);
              BufferedWriter writer = newBufferedWriter(get(PATH));
              for (String string : strings) {
                  writer.write(string);
                  writer.newLine();
              }
              writer.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>Unfortunately that commented piece of code for some reason doesn't work, while the one below it works just fine. That strings list always contains only proper result too. For file:</p>
  
  <pre><code>a
  //b
  c
  </code></pre>
  
  <p>The expected result should be:</p>
  
  <pre><code>a
  c
  </code></pre>
  
  <p>While the actual result is:</p>
  
  <pre><code>a
  c
  
  c
  </code></pre>
  
  <p>What is the source of this problem?</p>
  
  <p>It works when writing to a different file that the one we read from it.</p>