How to delete a folder with files using Java

后端 未结 28 3570
北荒
北荒 2020-11-28 05:36

I want to create and delete a directory using Java, but it isn\'t working.

File index = new File(\"/home/Work/Indexer1\");
if (!index.exists()) {
    index.m         


        
28条回答
  •  盖世英雄少女心
    2020-11-28 05:49

    In JDK 7 you could use Files.walkFileTree() and Files.deleteIfExists() to delete a tree of files. (Sample: http://fahdshariff.blogspot.ru/2011/08/java-7-deleting-directory-by-walking.html)

    In JDK 6 one possible way is to use FileUtils.deleteQuietly from Apache Commons which will remove a file, a directory, or a directory with files and sub-directories.

提交回复
热议问题