Back
Check out Apache's commons-io. It has a FileUtilsclass that mind does what you require.
FileUtils.deleteDirectory(new File("directory"));
From Java 7, we can certainly do this with reliable symlink detection. (I don't examine Apache's commons-io to have substantial symlink disclosure at this time, as it doesn't manage links on Windows designed with mklink.)
For the interest of history, here's a pre-Java 7 answer, which serves symlinks.
void delete(File f) throws IOException {if (f.isDirectory()) {for (File c : f.listFiles())delete(c);}if (!f.delete())throw new FileNotFoundException("Failed to delete file: " + f);}
void delete(File f) throws IOException {
if (f.isDirectory()) {
for (File c : f.listFiles())
delete(c);
}
if (!f.delete())
throw new FileNotFoundException("Failed to delete file: " + f);
31k questions
32.8k answers
501 comments
693 users