Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
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 (46k 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, 2019 in Java by Ritik (3.5k points)
0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Oct 29, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...