【问题标题】:clearing directory without excluded file in java在java中清除没有排除文件的目录
【发布时间】:2017-10-17 13:53:51
【问题描述】:

我尝试在没有文件的目录中删除文件,该路径在我的数组中

    public static boolean deleteDir(File dir, List<String> exclusionList) throws IOException {
    if (exclusionList != null && exclusionList.contains(dir.getCanonicalPath())) { // skip file

        System.out.println("Skipped: " + dir.getCanonicalPath());
        return true;
    }

    System.out.println("Deleting: " + dir.getCanonicalPath());
    if (dir.isDirectory()) {
        File[] children = dir.listFiles();
        boolean success = true;
        for (File element : children) {
            if (!deleteDir(element, exclusionList)) {
                success = false;
            }
        }
        return success;
    }

    return dir.delete();
}

这是我的删除功能,可以很好地删除 .txt .yml 等文件,但是当它必须删除文件夹(文件夹内容删除 perflecty)但文件夹存在时,我有很多空文件夹;/

【问题讨论】:

    标签: java


    【解决方案1】:

    如果我正确理解您的问题,您在删除文件后有许多空文件夹,例如.txt,您希望它们消失吗?

    这是一个想法:

    删除一个子(element)返回后,检查它是否是一个目录并且该目录中的文件数是否为零,然后应用:

    FileUtils.deleteDirectory(directory)

    if (element is a directory and is empty) {
        FileUtils.deleteDirectory(element);
    }
    

    这应该是您想要的。继续计算 if 语句中的逻辑,它应该删除 for 循环中的每个空目录。

    注意FileUtilshttp://commons.apache.org/proper/commons-io/ 的一部分

    【讨论】:

      【解决方案2】:

      问题是由如何处理目录引起的:

      System.out.println("Deleting: " + dir.getCanonicalPath());
      if (dir.isDirectory()) {
          File[] children = dir.listFiles();
          boolean success = true;
          for (File element : children) {
              if (!deleteDir(element, exclusionList)) {
                  success = false;
              }
          }
          return success;   // <= for directories the method returns here
      }
      
      return dir.delete();
      

      在目录中,该方法将为所有子元素递归调用deleteDir,并检查是否所有子元素都已成功删除。之后该方法简单地返回,而不删除目录本身。一个简单的解决方法是仅在某个子元素的删除失败时终止:

      System.out.println("Deleting: " + dir.getCanonicalPath());
      if (dir.isDirectory()) {
          File[] children = dir.listFiles();
          boolean success = true;
          for (File element : children) {
              if (!deleteDir(element, exclusionList)) {
                  success = false;
              }
          }
      
          // return only if some child couldn't be deleted.
          if(!success)
              return false;
      }
      
      // delete the directory itself (or the file, if a file is passed as parameter)
      return dir.delete();
      

      【讨论】:

      • 我改变了这个但我仍然有空文件夹,所有 .txt 文件都正确删除但文件夹仍然没有删除;/
      • @lenyAxe 这很奇怪。对我来说,这很好用。除排除目录外的所有目录都将被删除。您是否收到任何 IOException 或其他提示出了什么问题?可能是文件权限问题?
      • 权限很好,因为当我使用这些凭据并与 filezilla 连接时,我可以删除所有文件,任何异常;/ 我在 linux 上运行这个应用程序
      • 编辑。您的代码运行良好,我希望在方法 XD 中更改名称,谢谢 ;)
      • @lenyAxe 很高兴我能帮上忙 :)
      猜你喜欢
      • 2018-01-08
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 2015-11-06
      • 2017-10-13
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      相关资源
      最近更新 更多