现在的位置: 首页 > 综合 > 正文

java删除文件夹

2014年03月13日 ⁄ 综合 ⁄ 共 1251字 ⁄ 字号 评论关闭
 用递归实现的删除文件夹,很简单,功能也很强,非空文件夹也能删除!

package file;

import java.io.File;

public class DeleteAllFile {
    
public static void main(String args[]) {
        clearTmpFile(
new File("c:/forLiu"));
    }


    
private static void clearTmpFile(File tmpFile) {
        
if (!tmpFile.exists()) {
            System.out.println(
"file is not exist!");
            
return;
        }

        
if (tmpFile.isDirectory()) {
            File[] fileList 
= tmpFile.listFiles();
            
for (int i = 0; i < fileList.length; i++{
                
if (fileList[i].isDirectory()) {
                    clearTmpFile(fileList[i]);
                }

                
if (fileList[i].delete()) {
                    System.out.println(
"Temp file:""
                            
+ fileList[i].getAbsolutePath()
                            
+ "" has been deleted.");
                }

            }

            
if (tmpFile.delete()) {
                System.out.println(
"Temp file:"" + tmpFile.getAbsolutePath()
                        
+ "" has been deleted.");
            }

        }
 else {
            
if (tmpFile.delete()) {
                System.out.println(
"Temp file:"" + tmpFile.getAbsolutePath()
                        
+ "" has been deleted.");
            }

        }

    }

}

抱歉!评论已关闭.