JAVA中用递归函数来删除子目录

·  阅读 164
Java是一门强大的语言, 和其他语言一样,可以用递归函数来删除一个包含文件和子目录的目录.首先需要import相关的单元.
   import java.io.File;
  以下是递归函数内容:
   private Boolean pDeleteDirectory(String filePath) {
   Boolean bTotalRlt = true;
   File file = new File(filePath);
   File[] fileList = file.listFiles();
   String dirPath = null;
   if (fileList != null) {
   for (int i = 0; i < fileList.length; i++) {
   if (fileList.isFile()) {
   bTotalRlt = bTotalRlt && fileList.delete();
   }
   if (fileList.isDirectory()) {
   dirPath = fileList.getPath();
   bTotalRlt = bTotalRlt && pDeleteDirectory(dirPath);
   }
   }
   bTotalRlt = bTotalRlt && file.delete();
   }
   return bTotalRlt;
   }


分类:
后端
标签: