title: Apache commons-io FileUtils.deleteDirectory is not working properly
body: <p>I've a problem with commons-io <code>FileUtils.deleteDirectory(File)</code>. I simply call</p>
  
  <pre><code>FileUtils.deleteDirectory(new File("/tmp/dir"));
  </code></pre>
  
  <p>Directory structure is:</p>
  
  <pre><code>tmp/
   - dir/
      - a/
         - b/
            - c.txt
  </code></pre>
  
  <p>I try debug this with following results:</p>
  
  <ol>
  <li>I stop program in FileUtils before c.txt is deleted. <code>if (!file.delete())</code></li>
  <li>File is present and I can rename it (it is not locked I guess then).</li>
  <li><code>file.delete()</code> returns <code>true</code>, program continues a normal way (the file is still present, but now I can't rename it)</li>
  <li>I stop program before b/ directory is removed. <code>if (!directory.delete())</code></li>
  <li>c.txt is still present in this directory and <code>delete()</code> on directory returns false and "Unable to delete directory /tmp/dir/a/b/" exception is thrown</li>
  <li>When program ends the file is removed, but b/, a/, dir/ directories are not.</li>
  </ol>
  
  <p>Weird behavior for me is that c.txt file exists after deletion and invoking delete on his parent dir cause error then. The file is used only by Java program. Any suggestions? Any idea how to check in Java if some FileHandlers are still open for the file?</p>
  
  <p><strong>Update: fixed</strong></p>
  
  <p>I'm stupid jerk, I checked my code again and I found out that I miss close stream which previously read the file. I had a code for reading the input:</p>
  
  <pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream();
  InputStream is = new FileInputStream(new File("/tmp/dir/a/b/c.txt"));
  IOUtils.copy(is, baos); 
  String content = new String(baos.toByteArray());
  </code></pre>
  
  <p>and I change to (it works now):</p>
  
  <pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream();
  InputStream is = new FileInputStream(new File("/tmp/dir/a/b/c.txt"));
  IOUtils.copy(is, baos);
  // Close streams!
  baos.flush();
  baos.close();
  is.close(); // This is the most important!
  String content = new String(baos.toByteArray());
  </code></pre>
  
  <p>This is just example, I know that it is important to close all streams correctly (using try-finally). BufferedInputStream should be useful here too.</p>