Java Recursively Delete a Directory: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=External= * https://www.baeldung.com/java-delete-directory =Internal= * NIO 2 File API") |
|||
(3 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
=Internal= | =Internal= | ||
* [[Java#Subjects|Java]] | |||
* [[NIO 2 File API]] | * [[NIO 2 File API]] | ||
=Overview= | |||
<syntaxhighlight lang='java'> | |||
Files.walk(configFrameworkRoot.toPath()) | |||
.sorted(Comparator.reverseOrder()) | |||
.map(Path::toFile) | |||
.forEach(File::delete); | |||
</syntaxhighlight> | |||
=Remove a Directory in a Junit Test= | |||
<syntaxhighlight lang='java'> | |||
private Path testDirectory; | |||
@Before | |||
public void setUp() throws Exception { | |||
testDirectory = Files.createTempDirectory("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; | |||
} | |||
} | |||
</syntaxhighlight> |
Revision as of 23:29, 25 August 2020
External
Internal
Overview
Files.walk(configFrameworkRoot.toPath())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
Remove a Directory in a Junit Test
private Path testDirectory;
@Before
public void setUp() throws Exception {
testDirectory = Files.createTempDirectory("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;
}
}