title: Trying to create a directory immediately after a successful deleteIfExists throws AccessDenied Exception
body: <p>I am trying to create a file , and prior to that delete it if it exists. My problem is that whenever i have a successful delete operation and immediately after that try to create the same folder , it fails with AccessDenied. The method description(for both deleteIfExists and createDirectory) does not mention such behavior , so i guess i am doing something wrong .</p>
  
  <p>This is the code :</p>
  
  <pre><code>package nio2;
  import java.io.*;
  import java.nio.file.*;
  public class Test{
  
      public static void main(String[] args)
      {
          Path existing = Paths.get("nio2//alpha//inner.txt"); // already existing
          Path cpytarget = Paths.get("nio2//alphacpy//inner.txt"); // file to be created 
          Path target = Paths.get("nio2//alphacpy");//
          try{
              if(Files.exists(cpytarget))
              {
                  Files.list(target).forEach(Test::WrappedDeleteIfExists); // deleting files inside folder
                  System.out.println("Deleting the directory if it exists - alphaCpy\t" +  Files.deleteIfExists(target));//deleting
              }
              else
                  System.out.println("It does not exist, no need to delete anything");
              System.out.println("Creating alphaCpy\t" + Files.createDirectory(target));//creating
              System.out.println("copying inner.txt to the new directory\t" + Files.copy(existing,cpytarget));
          }catch(IOException e)
          {
              e.printStackTrace();
          }
      }
      public static void WrappedDeleteIfExists(Path in)
      {
          try{
              System.out.println("Deleting files inside the folder\t" + Files.deleteIfExists(in));
          }catch (IOException e)
          {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>So in a successful run ( when there is no delete ) . this is the output</p>
  
  <pre><code>It does not exist, no need to delete anything
  Creating alphaCpy       nio2\alphacpy
  copying inner.txt to the new directory  nio2\alphacpy\inner.txt
  </code></pre>
  
  <p>If i run it after that when the folder and file are already existing  i get the exception:</p>
  
  <pre><code>Deleting files inside the folder        true
  Deleting the directory if it exists - alphaCpy  true
  java.nio.file.AccessDeniedException: nio2\alphacpy
          at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
          at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
          at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
          at sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:504)
          at java.nio.file.Files.createDirectory(Files.java:674)
          at nio2.Test.main(Test.java:19)
  </code></pre>
  
  <p>Next run is again successful , as it has been already deleted and so on. So the question is what is causing the AccessDenied exception ? Have in mind that the file is not open/used, and the relative paths are valid.</p>
  
  <p>EDIT: Ok i managed to correct it but honestly i still can not explain to myself the initial problem. So i will be grateful if someone can help with that. I went from using a Stream to File[] in order to delete the file inside the folder. After i did that it works like a charm. </p>
  
  <p>Here is the corrected code:</p>
  
  <pre><code>public static void main(String[] args) throws InterruptedException
      {
          Path existing = Paths.get("E:/work/Java/Tests/alpha/inner.txt"); // already existing
          Path cpytarget = Paths.get("E:/work/Java/Tests/alphacpy/inner.txt"); // file to be created 
          Path target = Paths.get("E:/work/Java/Tests/alphacpy");//
          File fileTarget = new File("E:/work/Java/Tests/alphacpy");      
          try{
              if(Files.exists(cpytarget))
              {
                  WrappedDeleteIfExists(fileTarget.listFiles()); // CHANGED , no longer using Stream&lt;Path&gt; pipeline to go through the file list
                  // deleting files inside folder
                  System.out.println("Deleting the directory if it exists - alphaCpy\t" +  Files.deleteIfExists(target));//deleting
              }
              else
                  System.out.println("It does not exist, no need to delete anything");
              System.out.println(Files.exists(target));
  
              System.out.println("Creating alphaCpy\t" + Files.createDirectory(target));//creating
              System.out.println("copying inner.txt to the new directory\t" + Files.copy(existing,cpytarget));
          }catch(IOException e)
          {
              e.printStackTrace();
          }
      }
      // CHANGED - using File[] instead of Path
      public static void WrappedDeleteIfExists(File[] in)
      {
          for(int i =0;i&lt;in.length;i++)
          {
              System.out.println("Deleting files inside the folder\t" +in[i].delete());
          }
  
      }
  </code></pre>
  
  <p>Obviously the Stream operations are keeping it locked somehow, even after finishing, but this is not an IO Stream( or is it with Files.list()??) that i can close , so what can i do in order to make it work with Stream - its not something Closable or something that trying to force GC on makes sense.  </p>