title: Odd behaviour when deleting Files with Files.delete()
body: <p>Please consider the following example Java class (pom.xml below):</p>
  
  <pre class="lang-java prettyprint-override"><code>package test.filedelete;
  
  import java.io.ByteArrayInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.nio.file.Files;
  import java.nio.file.NoSuchFileException;
  import java.nio.file.Path;
  
  import org.apache.commons.io.IOUtils;
  
  public class Main
  {
      public static void main(String[] args) throws IOException
      {
          byte[] bytes = "testtesttesttesttesttesttesttesttesttest".getBytes();
          InputStream is = new ByteArrayInputStream(bytes);
  
          Path tempFileToBeDeleted = Files.createTempFile("test", "");
          OutputStream os = Files.newOutputStream(tempFileToBeDeleted);
          IOUtils.copy(is, os);
  
          deleteAndCheck(tempFileToBeDeleted);
  
          // breakpoint 1
          System.out.println("\nClosing stream\n");
  
          os.close();
  
          deleteAndCheck(tempFileToBeDeleted);
      }
  
      private static void deleteAndCheck(Path file) throws IOException
      {
          System.out.println("Deleting file: " + file);
          try
          {
              Files.delete(file);
          }
          catch (NoSuchFileException e)
          {
              System.out.println("No such file");
          }
          System.out.println("File really deleted: " + !Files.exists(file));
  
          System.out.println("Recreating deleted file ...");
          try
          {
              Files.createFile(file);
              System.out.println("Recreation successful");
          }
          catch (IOException e)
          {
              System.out.println("Recreation not possible, exception: " + e.getClass().getName());
          }
      }
  }
  </code></pre>
  
  <p>I write to a FileOutputStream and try to delete the file afterwards <em>without closing the Stream first</em>. This was my original problem, and of course wrong, but it leads to some strange observations.</p>
  
  <p>When you run the main Method on Windows 7 it produces the following output:</p>
  
  <pre class="lang-none prettyprint-override"><code>Deleting file: C:\Users\MSCHAE~1\AppData\Local\Temp\test6100073603559201768
  File really deleted: true
  Recreating deleted file ...
  Recreation not possible, exception: java.nio.file.AccessDeniedException
  
  Closing stream
  
  Deleting file: C:\Users\MSCHAE~1\AppData\Local\Temp\test6100073603559201768
  No such file
  File really deleted: true
  Recreating deleted file ...
  Recreation successful
  </code></pre>
  
  <ul>
  <li>Why does the first call to Files.delete() not throw an exception?</li>
  <li>Why does the following call to Files.exist() return false?</li>
  <li>Why is it not possible to create the file anew? </li>
  </ul>
  
  <p>Regarding the last question I noticed that the file is still visible in the Explorer when you stop at breakpoint 1. When you terminate the JVM then, the file will be deleted anyway. After closing the stream deleteAndCheck() works as expected.</p>
  
  <p>It seems to me that the deletion is not propagated to the OS before closing the stream and the Files API does not reflect this properly.</p>
  
  <p>Can someone explain exactly what's happening here?</p>
  
  <p><strong>pom.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;test&lt;/groupId&gt;
      &lt;artifactId&gt;filedelete&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-io&lt;/groupId&gt;
              &lt;artifactId&gt;commons-io&lt;/artifactId&gt;
              &lt;version&gt;2.4&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p><strong>Update for clarification</strong></p>
  
  <p>The file disappears in the Windows explorer, if the stream is closed AND Files.delete() is called - the last operation triggers -, or if Files.delete() has been called without closing the stream and the JVM is terminated.</p>