How to delete a folder with files using Java

后端 未结 28 3498
北荒
北荒 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:50

    Guava 21+ to the rescue. Use only if there are no symlinks pointing out of the directory to delete.

    com.google.common.io.MoreFiles.deleteRecursively(
          file.toPath(),
          RecursiveDeleteOption.ALLOW_INSECURE
    ) ;
    

    (This question is well-indexed by Google, so other people usig Guava might be happy to find this answer, even if it is redundant with other answers elsewhere.)

提交回复
热议问题