How to delete non empty directory in java

本文介绍两种在Java中删除非空目录的方法:一种是通过递归遍历并删除目录内的所有文件及子目录;另一种是利用Apache Commons IO库中的FileUtils类提供的deleteDirectory方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

摘要在这篇文章中,我们将看到如何删除非空的目录/文件夹。您可以使用java。文件的删除文件夹,但是如果它不是空的,你不能删除它。

有多种方法可以做到这一点:

  • Using java recursion(递归)
  • Using Apache common IO

1.Using java recursion(递归)

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

// 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 directly
                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());
        }
    }
2.Using Apache common IO:
删除非空文件夹是非常直接的。您只需要调用fileutils.deletedirectory()方法即可。
// Delete using Apache common IO
    public static void deleteUsingApacheIO(File file) {
        try {
            FileUtils.deleteDirectory(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
3.Java Program :




package cn.micai.io;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
 * 描述:How to delete non empty directory in java
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/7 10:54
 */
public class DeleteNonEmptyDirectoryMain {

    public static void main(String [] args) {
        System.out.println("Deleting using recursion");
        System.out.println("---------------");
        File folder= new File("D://workspace_spring_1");
        // Using recusrion
        try {
            delete(folder);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("---------------");
        System.out.println("Deleting using Apache IO");
        File folder2= new File("D://workspace_spring_2");
        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 directly
                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 file) {
        try {
            FileUtils.deleteDirectory(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
4.当我运行程序时,我得到了输出:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷彩的博客

你的鼓励将是我最大的创作动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值