title: How does "try-with-resources" actually work in the context of this program that deletes all files and folders below a given directory node
body: <p>This program deletes all files and folders beneath a given node. </p>
  
  <p>*<strong><em>EDIT</em>*</strong></p>
  
  <p>I have the following "test" directory structure on drive K:</p>
  
  <pre><code>Folder K:\garbage\
  f_00000b (file)
  f_00000f ( " )
  f_0000b3 ( " )
  dir1    [FOLDER]
  
  Folder K:\garbage\dir1\
  abc.pdf (file)
  b12.pdf (file)
  b36.pdf (file)
  dir2   [FOLDER]
  
  Folder K:\garbage\dir1\dir2\
  A.pdf   (file)
  a1.pdf  (file)
  A2.pdf  (file)
  </code></pre>
  
  <p>*<strong><em>END EDIT</em>*</strong></p>
  
  <p>Program works because I stumbled onto "try-with-resources", the line surrounded with all the ///////////////////////// being "the resource".</p>
  
  <pre><code>import java.io.IOError;
  import java.io.IOException;
  import java.nio.file.DirectoryNotEmptyException;
  import java.nio.file.DirectoryStream;
  import java.nio.file.Files;
  import java.nio.file.NotDirectoryException;
  import java.nio.file.Path;
  import java.nio.file.Paths;
  import java.util.ArrayList;
  import javax.swing.JOptionPane;
  
  public class CreatingDirectories {
  
      public static void main(String[] args) {
  
        deleteEverythingBelowThisNode(Paths.get("K:/garbage/dir1/dir2"));
      }
  
      static void deleteAllFilesIn(String ps){
  
        Path p = Paths.get(ps);
  
        try ////////////////////////////////////////////////////////////////
           (DirectoryStream&lt;Path&gt; paths = Files.newDirectoryStream(p)) /////
        { //////////////////////////////////////////////////////////////////
  
          for(Path q: paths)
            Files.delete(q);
  
        }catch(NotDirectoryException e){
          System.out.println("Not directory: " + p.toString() + "--" + e);
        }catch(DirectoryNotEmptyException e){
          System.out.println("Not empty: " + p.toString() + "--" + e);
        }
        catch(IOException e){
          System.out.println("IO: " + p.toString() + "--" + e);
        }
        catch(IOError e){
          System.out.println("IO ERROR: " + e);
        }
      }
  
      static void deleteEverythingBelowThisNode(Path p){
  
        String            sep        = p.getFileSystem().getSeparator();
        ArrayList&lt;String&gt; pathPieces = new ArrayList&lt;&gt;() ;
        String []         paths      = new String[p.getNameCount()];
  
        for(int i = 0 ; i &lt; p.getNameCount() ; i++){
          pathPieces.add(p.getName(i).toString());
          paths[i] = p.getRoot().toString();
        }
  
        for(int i = 0; i &lt; p.getNameCount() ; i++)
          for(int k = 0; k &lt;= i; k++)
            paths[i] += pathPieces.get(k) + sep;
  
        for(int k = p.getNameCount() - 1; k &gt;= 0; k--)
          deleteAllFilesIn(paths[k]);
      }
  }
  </code></pre>
  
  <p>I understand that "try-with-resources" is necessary: program works with it, not without it.</p>
  
  <p>But I don't understand why and I don't know how it solved the original problem, which I now describe.</p>
  
  <p>I originally had situated the "resources" above the try-block, like this, which seems perfectly natural:</p>
  
  <pre><code>      DirectoryStream&lt;Path&gt; paths = Files.newDirectoryStream(p);
        try {
             for...
  </code></pre>
  
  <p>With the program structure otherwise identical except for moving that one line as shown above, all files and subfolder had been successfully deleted from a folder, but <code>DirectoryNotEmptyException</code> was thrown. Windows Explorer confirmed that the directory was empty after the program terminated because of the exception.</p>
  
  <p>Why was the exception thrown on an empty directory?</p>
  
  <p>From <a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html" rel="nofollow">the horse's mouth</a>, "The try-with-resources statement ... declares ... an object that ... is closed at the end of the statement."</p>
  
  <p>Closing happens at the end of the statement, so at the end of the loop. How did the exception not occur even with try-with-resources?</p>
  
  <p>As it now is, after looping through the entire node, everything beneath it has been deleted. </p>
  
  <p>So what did try-with-resources actually do to enable deleting an empty folder that could not be deleted without try-with-resources?</p>
  
  <p>These don't seem like stupid questions or a trivial situation.</p>
  
  <p>Did a <code>DirectoryNotEmptyException</code> actually occur anyway, but try-with-resources somehow handled it? I can't believe I'm asking that, since it does seem like a stupid question, but what actually happened to make the program run as expected? </p>