How to delete a folder with files using Java

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

    Here is a simple way to do it :

    public void deleteDirectory(String directoryPath)  {
          new Thread(new Runnable() {
              public void run() {
                 for(String e: new File(directoryPath).list()) {
                     if(new File(e).isDirectory()) 
                         deleteDirectory(e);
                     else 
                         new File(e).delete();
                 }
              }
          }).start();
      }
    

提交回复
热议问题