public static void deleteDirectory(String path) throws IOException { deleteDirectory(Paths.get(path)); }
public static void deleteDirectory(Path path) throws IOException { clearDirectory(path.toString()); Files.deleteIfExists(path); }
@Test public void shouldDeleteDirectory() throws IOException { //given final Path dirToDelete = Files.createTempDirectory("jlineup-fileutils-test"); Files.createDirectories(dirToDelete.resolve("one/two/three")); //when deleteDirectory(dirToDelete); //then assertThat(Files.exists(dirToDelete), is(false)); }
private void createOrClearDirectoryBelowWorkingDir(String workingDirectory, String subDirectory) throws IOException { try { final String subDirectoryPath = workingDirectory + "/" + subDirectory; createDirIfNotExists(subDirectoryPath); clearDirectory(subDirectoryPath); } catch (IOException e) { throw new IOException("Could not create or open " + subDirectory + " directory.", e); } }
@After public void cleanUp() throws IOException { FileUtils.deleteDirectory(tempJLineupDirectory); }
public static void clearDirectory(String path) throws IOException { Path directory = Paths.get(path); Files.newDirectoryStream(directory).forEach(file -> { try { if (Files.isDirectory(file)) { clearDirectory(file.toAbsolutePath().toString()); } Files.delete(file); } catch (IOException e) { throw new UncheckedIOException(e); } }); }
@After public void cleanup() throws IOException { FileUtils.deleteDirectory(tempDirectory); }
@Test public void shouldClearDirectory() throws IOException { //given final Path dirToClear = Files.createTempDirectory("jlineup-fileutils-test"); Files.createFile(dirToClear.resolve(Paths.get("test1"))); Files.createFile(dirToClear.resolve(Paths.get("test2"))); Files.createFile(dirToClear.resolve(Paths.get("test3"))); //when clearDirectory(dirToClear.toString()); //then assertThat(dirToClear.toFile().list().length, is(0)); Files.delete(dirToClear); }