Java Recursively Delete a Directory: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 13: Line 13:
         .map(Path::toFile)
         .map(Path::toFile)
         .forEach(File::delete);
         .forEach(File::delete);
</syntaxhighlight>
=Remove a Directory in a Junit Test=
<syntaxhighlight lang='java'>
@After
public void cleanup() throws Exception {
  if (testDirectory != null) {
    Files.walk(testDirectory)
          .sorted(Comparator.reverseOrder())
          .map(Path::toFile)
          .forEach(File::delete);
    assertFalse(Files.exists(testDirectory));
    testDirectory = null;
  }
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 23:12, 18 June 2020

External

Internal

Overview

Files.walk(configFrameworkRoot.toPath())
         .sorted(Comparator.reverseOrder())
         .map(Path::toFile)
         .forEach(File::delete);

Remove a Directory in a Junit Test

@After
public void cleanup() throws Exception {

  if (testDirectory != null) {

     Files.walk(testDirectory)
          .sorted(Comparator.reverseOrder())
          .map(Path::toFile)
          .forEach(File::delete);

     assertFalse(Files.exists(testDirectory));
     testDirectory = null;
  }
}