How To Delete Directory In Java

本文介绍如何使用Java代码递归删除目录及其示例代码,包括检查目录是否存在、判断目录是否为空以及递归删除目录下所有子目录和文件的过程。

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

To delete a directory, you can simply use the File.delete(), but the directory must be empty in order to delete it.

Often times, you may require to perform recursive delete in a directory, which means all it’s sub-directories and files should be delete as well, see below example :

Directory recursive delete example

Delete the directory named “C:\\mkyong-new“, and all it’s sub-directories and files as well. The code is self-explanatory and well documented, it should be easy to understand.

package com.mkyong.file;
 
import java.io.File;
import java.io.IOException;
 
public class DeleteDirectoryExample
{
    private static final String SRC_FOLDER = "C:\\mkyong-new";
 
    public static void main(String[] args)
    {	
 
    	File directory = new File(SRC_FOLDER);
 
    	//make sure directory exists
    	if(!directory.exists()){
 
           System.out.println("Directory does not exist.");
           System.exit(0);
 
        }else{
 
           try{
 
               delete(directory);
 
           }catch(IOException e){
               e.printStackTrace();
               System.exit(0);
           }
        }
 
    	System.out.println("Done");
    }
 
    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());
    	}
    }
}

Result
File is deleted : C:\mkyong-new\404.php
File is deleted : C:\mkyong-new\archive.php
...
Directory is deleted : C:\mkyong-new\includes
File is deleted : C:\mkyong-new\index.php
File is deleted : C:\mkyong-new\index.php.hacked
File is deleted : C:\mkyong-new\js\hoverIntent.js
File is deleted : C:\mkyong-new\js\jquery-1.4.2.min.js
File is deleted : C:\mkyong-new\js\jquery.bgiframe.min.js
Directory is deleted : C:\mkyong-new\js\superfish-1.4.8\css
Directory is deleted : C:\mkyong-new\js\superfish-1.4.8\images
Directory is deleted : C:\mkyong-new\js\superfish-1.4.8
File is deleted : C:\mkyong-new\js\superfish-navbar.css
...
Directory is deleted : C:\mkyong-new
Done
转自: http://www.mkyong.com/java/how-to-delete-directory-in-java/

### 使用Java POI库删除Word文档中的段落 为了实现从Word文档中删除特定段落的功能,可以利用Apache POI库来操作`.docx`文件。下面提供一段示例代码用于展示如何通过指定索引来移除某个段落。 ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class DeleteParagraphExample { public static void main(String[] args) throws IOException { // 加载现有的Word文档 try (FileInputStream fis = new FileInputStream("example.docx"); XWPFDocument document = new XWPFDocument(fis)) { int indexToDelete = 2; // 假设要删除第三个段落(索引从0开始) if(indexToDelete >= 0 && indexToDelete < document.getNumberOfParagraphs()){ // 获取所有段落列表并从中移除目标项 document.removeBodyElement(indexToDelete); // 将修改后的文档保存到新的文件中 try (FileOutputStream fos = new FileOutputStream("modified_example.docx")) { document.write(fos); } }else{ System.out.println("Index out of bounds."); } } } } ``` 上述程序展示了基本的操作流程:加载文档、定位待删段落的位置并通过调用`removeBodyElement()`方法将其移除,最后将更改写回磁盘上的新文件[^1]。 对于更复杂的场景,比如基于内容匹配而不是固定位置来决定哪些段落应该被删除,则可能需要遍历整个文档结构并对每个段落的内容进行检查后再执行相应的逻辑处理[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值