Skip to content

Commit ec9a27a

Browse files
committed
BAEL-3651: Make sure the directory is always initially empty
1 parent 61a972e commit ec9a27a

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

core-java-modules/core-java-io-apis/src/test/java/com/baeldung/file/FileClassUnitTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,16 @@ public void givenDataWritten_whenWrite_thenFreeSpaceReduces() {
156156

157157
private static File makeDir(String name) {
158158
File directory = new File(name);
159-
directory.mkdir();
160-
if (directory.isDirectory()) {
159+
160+
// If the directory already exists, make sure we create it 'from scratch', i.e. all the files inside are deleted first
161+
if (directory.exists()) {
162+
removeDir(directory);
163+
}
164+
165+
if (directory.mkdir()) {
161166
return directory;
162167
}
168+
163169
throw new RuntimeException("'" + name + "' not made!");
164170
}
165171

0 commit comments

Comments
 (0)