---
title: "java实现删除文件夹及文件夹下所有文件"
author: "fuyangchang"
date: "2007-06-29T10:26:00Z"
tags: []
slug: "java"
draft: false
lastmod: "2007-06-29T10:26:00Z"
id: 104064
url: "https://csdn.iteye.com/blog/104064"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.io.File;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class FileOperate ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    /\** \*//\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     * @param args  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    public static void main(String\[] args) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        String str="d:\\bb";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File file=new File(str);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File fileb=new File("d:\\bb.rar");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        FileOperate fp=new FileOperate();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (fp.deletedir(file))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println("success");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            System.out.println("failed!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public boolean deletefile(File f)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if (f.isFile())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            f.delete();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    public boolean deletedir(File f)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{ if(f.isDirectory())  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        File\[] files = f.listFiles();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        for(int i=0;i&lt;files.length;i++)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)        ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            if(files\[i].isDirectory()) deletedir(files\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)            else deletefile(files\[i]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)        }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        f.delete();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

   
在main函数调用的时候,要注意先new一个类,然后用这个对象去调用类中声名的其他的public方法.在本实现中还实现了文件重命名,只需要调用renameTo()方法即可.