0 votes
1 view
in Java by (3.4k points)

Check out Apache's commons-io. It has a FileUtilsclass that mind does what you require.

FileUtils.deleteDirectory(new File("directory"));

1 Answer

0 votes
by (39.8k points)

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);

}

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 13 in Java by Ritik (3.5k points)
0 votes
1 answer
asked Sep 29 in Java by Shubham (3.9k points)
0 votes
1 answer
asked 1 hour ago in Java by Anvi (7.9k points)
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...