Skip to content

Commit

Permalink
#813 Implement ability to create and delete directories recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
ipolevoy committed Dec 14, 2018
1 parent 17752a0 commit 711280f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 1 deletion.
77 changes: 77 additions & 0 deletions javalite-common/src/main/java/org/javalite/common/Util.java
Expand Up @@ -16,6 +16,8 @@
package org.javalite.common;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;

/**
Expand Down Expand Up @@ -525,4 +527,79 @@ public static Properties readProperties(String fileOrResource) throws IOExceptio
}
return props;
}



/**
* Creates directories recursively.
*
* <p>For instance, this:</p>
* <pre>
* Util.createTree("dir1/dir2/dir3);
* </pre>
*
* will create "dir1/dir2/dir2".
*
* <p>
* If a directory already exists, it will be silently ignored.
* </p>
*
* <p></p>
*
* <p>
* <strong>NOTE:</strong> Use '/' as the path separator on *nix and Windows.
* </p>
*
* @param path a list of directories where each is a sub-directory of a previous.
*
*/
public static void createTree(Path path) throws IOException {

// NOTE, the Paths.get("start", ... others) is super annoying, so implemented with strings in a somewhat hacky way.
// Not happy:(
boolean absolute = path.toString().startsWith("/");

String[] parts = Util.split(path.toString(), "/");
for (int i = 0; i < parts.length; i++) {
StringBuilder fullPath = new StringBuilder();
for(int x = 0; x <= i; x++){
fullPath.append(parts[x]).append("/"); // should be OK for *nix and Windows
}
File dir = new File((absolute ? "/" : "") + fullPath.toString());
if(!dir.exists()){
if(!dir.mkdir()){
throw new IOException("Failed to create a directory: " + fullPath);
}
}
}
}

/**
* Deletes a directory recursively with all its contents. Will ignore files and directories if they are disappear during the oprtation.
*
* @param directory directory to delete.
*/
public static void recursiveDelete(Path directory) throws IOException {

try {
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws java.io.IOException {
try {
Files.delete(file);
} catch (NoSuchFileException ignore) {}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, java.io.IOException exc) throws java.io.IOException {
try{
Files.delete(dir);
}catch (NoSuchFileException ignore){}
return FileVisitResult.CONTINUE;
}
});
} catch (NoSuchFileException ignore) {}

}
}
26 changes: 25 additions & 1 deletion javalite-common/src/test/java/org/javalite/common/UtilTest.java
Expand Up @@ -18,7 +18,9 @@
package org.javalite.common;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -133,7 +135,7 @@ public void testEmptyArray() {
@Test
public void testEmptyCollection() {
a(Util.empty((Collection<Object>) null)).shouldBeTrue();
a(Util.empty(new ArrayList<Object>())).shouldBeTrue();
a(Util.empty(new ArrayList())).shouldBeTrue();
a(Util.empty(Collections.list("Hello"))).shouldBeFalse();
}

Expand Down Expand Up @@ -228,4 +230,26 @@ public void testJoinAndRepeat() {
Util.joinAndRepeat(sb, "na", ", ", 16);
the(sb.toString()).shouldBeEqual("na, na, na, na, na, na, na, na, na, na, na, na, na, na, na, na");
}

@Test
public void shouldCreateAndDeleteDirectories() throws IOException {

String root = "target/dir1";

Util.recursiveDelete(Paths.get(root)); // will ignore that the directory does not exist yet
the(Paths.get(root).toFile().exists()).shouldBeFalse();

Util.createTree(Paths.get(root + "/dir2/dir3"));
the(Paths.get("target/dir1/dir2/dir3").toFile().exists()).shouldBeTrue();
Util.recursiveDelete(Paths.get("target/dir1/dir2"));
the(Paths.get("target/dir1/dir2").toFile().exists()).shouldBeFalse();
the(Paths.get("target/dir1/dir2/dir3").toFile().exists()).shouldBeFalse();
the(Paths.get("target/dir1/").toFile().exists()).shouldBeTrue();

//test absolute path
String curDir = new File(".").getCanonicalPath();
Util.createTree( Paths.get(curDir + "/target/dir4"));
the(Paths.get(curDir + "/target/dir4").toFile().exists()).shouldBeTrue();
}

}

0 comments on commit 711280f

Please sign in to comment.