如何在Java中删除非空目录

时间:2020-02-23 14:34:50  来源:igfitidea点击:

在本教程中,我们将看到如何删除非空的目录/文件夹。
我们可以使用java.io.file的删除eMPy文件夹,但如果它不是空的,则无法删除它。

有多种方法可以做到。

  • 使用Java递归
  • 使用apache common io

使用Java递归:

这样做是非常直接的。
它将遍历文件夹中的所有文件。
如果它是文件,那么我们可以直接删除它,但如果它在内部找到文件夹,那么我们再次调用相同的方法。

//Delete using recursion
 public static void delete(File file)
   throws IOException{
 
  if(file.isDirectory()){
 
   //We can directly delete if we found empty directory
   if(file.list().length==0){
 
    file.delete();
    System.out.println("Deleting folder : " 
      + file.getAbsolutePath());
 
   }else{
 
    //list all the files in directory
    File files[] = file.listFiles();
 
    for (File temp : files) {
     //recursive delete
     delete(temp);
    }
 
    //Check directory again, if we find it empty, delete it
    if(file.list().length==0){
     file.delete();
     System.out.println("Deleting folder : " 
       + file.getAbsolutePath());
    }
   }
 
  }else{
   //if file, then we can directly delete it
   file.delete();
   System.out.println("Deleting file  : " + file.getAbsolutePath());
  }
 }

使用apache common io:

删除非空文件夹非常简单。
我们只需要调用fileutils.deletedirectory()方法。

//Delete using Apache common IO
 public static void deleteUsingApacheIO(File f)
 {
  try {
   FileUtils.deleteDirectory(f);
  } catch (IOException e) {
 
   e.printStackTrace();
  }
 }

Java程序:

package org.igi.theitroad;
 
import java.io.File;
import java.io.IOException;
 
import org.apache.commons.io.FileUtils;
 
public class DeleteNonEmptyDirectoryMain {
 
 public static void main(String[] args) {
  
  
  System.out.println("Deleting using recursion");
  System.out.println("---------------");
  File folder= new File("/Users/igi/Desktop/Blog/temp1");
  //Using recusrion
  try {
   delete(folder);
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  System.out.println("---------------");
  System.out.println("Deleting using Apache IO");
  File folder2= new File("/Users/igi/Desktop/Blog/temp2");
  deleteUsingApacheIO(folder2);
  System.out.println("Deleting folder : " 
    + folder2.getAbsolutePath());
 }
 
 //Delete using recursion
 public static void delete(File file)
   throws IOException{
 
  if(file.isDirectory()){
 
   //We can directly delete if we found empty directory
   if(file.list().length==0){
 
    file.delete();
    System.out.println("Deleting folder : " 
      + file.getAbsolutePath());
 
   }else{
 
    //list all the files in directory
    File files[] = file.listFiles();
 
    for (File temp : files) {
     //recursive delete
     delete(temp);
    }
 
    //Check directory again, if we find it empty, delete it
    if(file.list().length==0){
     file.delete();
     System.out.println("Deleting folder : " 
       + file.getAbsolutePath());
    }
   }
 
  }else{
   //if file, then we can directly delete it
   file.delete();
   System.out.println("Deleting file  : " + file.getAbsolutePath());
  }
 }
 
 //Delete using Apache common IO
 public static void deleteUsingApacheIO(File f)
 {
  try {
   FileUtils.deleteDirectory(f);
  } catch (IOException e) {
 
   e.printStackTrace();
  }
 }
}

当我在上面的程序上运行时,我得到了以下输出

Deleting using recursion
--------------
Deleting file  : /Users/igi/Desktop/Blog/temp1/.DS_Store
Deleting file  : /Users/igi/Desktop/Blog/temp1/BinaryTree/.DS_Store
Deleting file  : /Users/igi/Desktop/Blog/temp1/BinaryTree/verticalSum.jpg.png
Deleting file  : /Users/igi/Desktop/Blog/temp1/BinaryTree/VerticalSumOrder.png
Deleting folder : /Users/igi/Desktop/Blog/temp1/BinaryTree
Deleting file  : /Users/igi/Desktop/Blog/temp1/jersey json/.DS_Store
Deleting file  : /Users/igi/Desktop/Blog/temp1/jersey json/jerseyJsonCountry.png
Deleting file  : /Users/igi/Desktop/Blog/temp1/jersey json/jerseyJsonCountryID.png
Deleting file  : /Users/igi/Desktop/Blog/temp1/jersey json/jerseyMavenBuild.png
Deleting file  : /Users/igi/Desktop/Blog/temp1/jersey json/jerseyMavenCleanInstall.png
Deleting file  : /Users/igi/Desktop/Blog/temp1/jersey json/jerseyProjectStructure.png
Deleting folder : /Users/igi/Desktop/Blog/temp1/jersey json
Deleting folder : /Users/igi/Desktop/Blog/temp1
--------------
Deleting using Apache IO
Deleting folder : /Users/igi/Desktop/Blog/temp2