删除非空文件夹的递归算法

来源:互联网 发布:js switch case字符串 编辑:程序博客网 时间:2024/04/29 06:16
public static boolean deleteFile(String filePath) throws FileNotFoundException,
      IOException {
    try {
      File file = new File(filePath);
      if (!file.isDirectory()) {
        file.delete();
      }
      else if (file.isDirectory()) {
        String[] filelist = file.list();
        for (int i = 0; i < filelist.length; i++) {
          File subfile = new File(filePath+ "//" + filelist[i]);
          if (!subfile .isDirectory())
            subfile .delete();
          else if (subfile .isDirectory())
            deleteFile(filePath+ "//" + filelist[i]);
        }
        file.delete();
      }
    }
    catch (FileNotFoundException e) {
      Log.debug("deleteFile() Exception:" + e.getMessage());
    }
    return true;
  }
原创粉丝点击