【问题标题】:How to delete a folder containing other folders in Java? [duplicate]如何删除包含Java中其他文件夹的文件夹? [复制]
【发布时间】:2014-02-15 12:13:41
【问题描述】:

这是我试过的代码:

import java.io.*;
public class file03 {
    public static void main(String[] args) {
        File f1 = new File("C:/tempo1/tempo");
        f1.mkdirs();
        File f2 = new File("C:/test");
        if(!f2.exists()) {
            f2.mkdir();
        }
        f1 = new File("C:/tempo1/kempo");
        f1.mkdirs();
        f1 = new File("C:/tempo1");
        String[] t = {};
        if(f1.exists()) {
            t = f1.list();
            System.out.println(t.length + " files found");
        }
        for(int i = 0; i < t.length; i++) {
            System.out.println(t[i]);
        }
        try {
            Thread.sleep(3000);
        }
        catch(Exception e) {}
        f2.delete();
        f2 = new File("C:/tempo1/test.txt");
        try {
            f2.createNewFile();
        }
        catch(Exception e) {}
        try {
            Thread.sleep(7000);
        }
        catch(Exception e) {}
        File f3 = new File("C:/tempo1/renametesting.txt");
        f2.renameTo(f3);
        try {
            Thread.sleep(5000);
        }
        catch(Exception e) {}
        f3 = new File("C:/tempo1");
        f3.delete();
    }
}

我注意到,当文件夹 test 被删除时,文件夹 tempo1 并没有被删除。是因为它包含其他文件夹和文件吗?如果是这样,我该如何删除它? 我正在使用 BlueJ IDE。

【问题讨论】:

标签: java file file-io directory bluej


【解决方案1】:

在删除该文件夹的所有文件之前,无法删除该文件夹。

首先删除该文件夹中的所有文件,然后删除该文件夹

这是删除文件夹的代码..

您只需要传递文件夹的路径

public static void delete(File file)
            throws IOException {

        if (file.isDirectory()) {

            //directory is empty, then delete it
            if (file.list().length == 0) {

                file.delete();
//                System.out.println("Directory is deleted : "+ file.getAbsolutePath());

            } else {

                //list all the directory contents
                String files[] = file.list();

                for (String temp : files) {
                    //construct the file structure
                    File fileDelete = new File(file, temp);

                    //recursive delete
                    delete(fileDelete);
                }

                //check the directory again, if empty then delete it
                if (file.list().length == 0) {
                    file.delete();
//                    System.out.println("Directory is deleted : " + file.getAbsolutePath());
                }
            }

        } else {
            //if file, then delete it
            file.delete();
//            System.out.println("File is deleted : " + file.getAbsolutePath());
        }
    }

【讨论】:

  • 非常感谢。但是没有更小或更简单的东西吗?
  • @user3177527 它更小。删除 cmets
  • 我想要单行或两行代码。:P
【解决方案2】:
public class DeleteFolder {
/**
 * Delete a folder and all content folder & files.
 * @param folder
 */
  public void rmdir(final File folder) {     
      if (folder.isDirectory()) {           //Check if folder file is a real folder
          File[] list = folder.listFiles(); //Storing all file name within array
          if (list != null) {               //Checking list value is null or not to check folder containts atlest one file
              for (int i = 0; i < list.length; i++) {    
                  File tmpF = list[i];
                  if (tmpF.isDirectory()) {   //if folder  found within folder remove that folder using recursive method
                      rmdir(tmpF);
                  }
                  tmpF.delete();             //else delete file
              }
          }
          if (!folder.delete()) {            //if not able to delete folder print message
            System.out.println("can't delete folder : " + folder);
          }
      }
  }
}

【讨论】:

  • 完全按照我的方式去做!
  • -1。 “只是代码”的答案对我不利。因为你展示了如何做,但你没有解释你在做什么,所以 OP 没有从这个答案中学到任何东西,他只是在复制你的代码,这不是本网站的目的。
  • 任何java coder都能看懂这小段代码。现在我为你添加评论。
  • @anupammaiti 不,如果他不知道递归或者他刚开始编程,他可能会发现很难理解代码。
  • 非常感谢。但是没有更直接的吗?
【解决方案3】:

要删除包含文件的文件夹,无需循环或递归搜索。可以直接使用:

FileUtils.deleteDirectory(<File object of directory>);

此功能将删除文件夹及其中的所有文件

【讨论】:

  • 非常感谢。这似乎或多或少是我想要的。这是一个内置函数吗?
  • 我试过了:FileUtils.deleteDirectory(f3);它说找不到符号 FileUtils。
  • 是的,它是 Apache commons 的一部分:org.apache.commons.io.FileUtils
  • 导入 org.apache.commons.io.FileUtils;导入java.io.File;导入 java.io.IOException; public class DeleteDirectory { public static void main(String[] args) { try { File directory = new File("/home/foobar/Temp/Data"); // // 递归删除一个目录。当删除过程失败时, // IOException 被抛出,这就是我们捕获异常的原因。 // FileUtils.deleteDirectory(目录); } catch (IOException e) { e.printStackTrace(); } } }
  • 怎么做:import org.apache.commons.io.FileUtils;它给出了一个错误。 apache commons到底是什么?
【解决方案4】:

【讨论】:

  • 我试过了:FileUtils.deleteDirectory(f3);它说找不到符号 FileUtils。不是内置的吗?
  • 您必须下载 commons io 库并将其添加到您的类路径中。
猜你喜欢
  • 2011-11-24
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 2011-01-15
  • 2015-09-14
  • 1970-01-01
相关资源
最近更新 更多