@Test public void testDeleteRecursively() throws IOException { File tempDir = Files.createTempDir(); tempDir.deleteOnExit(); assertTrue(tempDir.exists()); File subFile1 = new File(tempDir, "subFile1"); Files.write(SOME_BYTES, subFile1); assertTrue(subFile1.exists()); File subDir1 = new File(tempDir, "subDir1"); IOUtils.mkdirs(subDir1); File subFile2 = new File(subDir1, "subFile2"); Files.write(SOME_BYTES, subFile2); assertTrue(subFile2.exists()); File subDir2 = new File(subDir1, "subDir2"); IOUtils.mkdirs(subDir2); IOUtils.deleteRecursively(tempDir); assertFalse(tempDir.exists()); assertFalse(subFile1.exists()); assertFalse(subDir1.exists()); assertFalse(subFile2.exists()); assertFalse(subDir2.exists()); }
public static void deleteRecursively(File dir) throws IOException { if (dir == null || !dir.exists()) { return; } Deque<File> stack = new ArrayDeque<File>(); stack.push(dir); while (!stack.isEmpty()) { File topElement = stack.peek(); if (topElement.isDirectory()) { File[] directoryContents = topElement.listFiles(); if (directoryContents != null && directoryContents.length > 0) { for (File fileOrSubDirectory : directoryContents) { stack.push(fileOrSubDirectory); } } else { delete(stack.pop()); } } else { delete(stack.pop()); } } }
IOUtils { public static void deleteRecursively(File dir) throws IOException { if (dir == null || !dir.exists()) { return; } Deque<File> stack = new ArrayDeque<File>(); stack.push(dir); while (!stack.isEmpty()) { File topElement = stack.peek(); if (topElement.isDirectory()) { File[] directoryContents = topElement.listFiles(); if (directoryContents != null && directoryContents.length > 0) { for (File fileOrSubDirectory : directoryContents) { stack.push(fileOrSubDirectory); } } else { delete(stack.pop()); } } else { delete(stack.pop()); } } } }
IOUtils { public static void deleteRecursively(File dir) throws IOException { if (dir == null || !dir.exists()) { return; } Deque<File> stack = new ArrayDeque<File>(); stack.push(dir); while (!stack.isEmpty()) { File topElement = stack.peek(); if (topElement.isDirectory()) { File[] directoryContents = topElement.listFiles(); if (directoryContents != null && directoryContents.length > 0) { for (File fileOrSubDirectory : directoryContents) { stack.push(fileOrSubDirectory); } } else { delete(stack.pop()); } } else { delete(stack.pop()); } } } private  IOUtils(); }
IOUtils { public static void deleteRecursively(File dir) throws IOException { if (dir == null || !dir.exists()) { return; } Deque<File> stack = new ArrayDeque<File>(); stack.push(dir); while (!stack.isEmpty()) { File topElement = stack.peek(); if (topElement.isDirectory()) { File[] directoryContents = topElement.listFiles(); if (directoryContents != null && directoryContents.length > 0) { for (File fileOrSubDirectory : directoryContents) { stack.push(fileOrSubDirectory); } } else { delete(stack.pop()); } } else { delete(stack.pop()); } } } private  IOUtils(); static void deleteRecursively(File dir); static void delete(File file); static InputStream openMaybeDecompressing(File file); static Reader openReaderMaybeDecompressing(File file); static void copyStreamToFile(InputStream in, File file); static void copyURLToFile(URL url, File file); static void mkdirs(File dir); static GZIPOutputStream buildGZIPOutputStream(OutputStream delegate); static Writer buildGZIPWriter(OutputStream delegate); static Writer buildGZIPWriter(File file); static BufferedReader buffer(Reader maybeBuffered); static boolean isGZIPFileEmpty(File f); }
IOUtils { public static void deleteRecursively(File dir) throws IOException { if (dir == null || !dir.exists()) { return; } Deque<File> stack = new ArrayDeque<File>(); stack.push(dir); while (!stack.isEmpty()) { File topElement = stack.peek(); if (topElement.isDirectory()) { File[] directoryContents = topElement.listFiles(); if (directoryContents != null && directoryContents.length > 0) { for (File fileOrSubDirectory : directoryContents) { stack.push(fileOrSubDirectory); } } else { delete(stack.pop()); } } else { delete(stack.pop()); } } } private  IOUtils(); static void deleteRecursively(File dir); static void delete(File file); static InputStream openMaybeDecompressing(File file); static Reader openReaderMaybeDecompressing(File file); static void copyStreamToFile(InputStream in, File file); static void copyURLToFile(URL url, File file); static void mkdirs(File dir); static GZIPOutputStream buildGZIPOutputStream(OutputStream delegate); static Writer buildGZIPWriter(OutputStream delegate); static Writer buildGZIPWriter(File file); static BufferedReader buffer(Reader maybeBuffered); static boolean isGZIPFileEmpty(File f); static final FilenameFilter CSV_COMPRESSED_FILTER; static final FilenameFilter NOT_HIDDEN; }