`

java_demo ---> Delete file folder

阅读更多
package cn.robot.deleteFolder;

import java.io.File;

public class DeleteFileUtil {    
    /**  
     * 删除文件,可以是单个文件或文件夹  
     * @param   fileName    待删除的文件名  
     * @return 文件删除成功返回true,否则返回false  
     */   
    public static boolean delete(String fileName){    
        File file = new File(fileName);    
        if(!file.exists()){    
            System.out.println("删除文件失败:"+fileName+"文件不存在");    
            return false;    
        }else{    
            if(file.isFile()){    
                    
                return deleteFile(fileName);    
            }else{    
                return deleteDirectory(fileName);    
            }    
        }    
    }    
        
    /**  
     * 删除单个文件  
     * @param   fileName    被删除文件的文件名  
     * @return 单个文件删除成功返回true,否则返回false  
     */   
    public static boolean deleteFile(String fileName){    
        File file = new File(fileName);    
        if(file.isFile() && file.exists()){    
            file.delete();    
            System.out.println("删除单个文件"+fileName+"成功!");    
            return true;    
        }else{    
            System.out.println("删除单个文件"+fileName+"失败!");    
            return false;    
        }    
    }    
        
    /**  
     * 删除目录(文件夹)以及目录下的文件  
     * @param   dir 被删除目录的文件路径  
     * @return  目录删除成功返回true,否则返回false  
     */   
    public static boolean deleteDirectory(String dir){    
        //如果dir不以文件分隔符结尾,自动添加文件分隔符    
        if(!dir.endsWith(File.separator)){    
            dir = dir+File.separator;    
        }    
        File dirFile = new File(dir);    
        //如果dir对应的文件不存在,或者不是一个目录,则退出    
        if(!dirFile.exists() || !dirFile.isDirectory()){    
            System.out.println("删除目录失败"+dir+"目录不存在!");    
            return false;    
        }    
        boolean flag = true;    
        //删除文件夹下的所有文件(包括子目录)    
        File[] files = dirFile.listFiles();    
        for(int i=0;i<files.length;i++){    
            //删除子文件    
            if(files[i].isFile()){    
                flag = deleteFile(files[i].getAbsolutePath());    
                if(!flag){    
                    break;    
                }    
            }    
            //删除子目录    
            else{    
                flag = deleteDirectory(files[i].getAbsolutePath());    
                if(!flag){    
                    break;    
                }    
            }    
        }    
            
        if(!flag){    
            System.out.println("删除目录失败");    
            return false;    
        }    
            
        //删除当前目录    
        if(dirFile.delete()){    
            System.out.println("删除目录"+dir+"成功!");    
            return true;    
        }else{    
            System.out.println("删除目录"+dir+"失败!");    
            return false;    
        }    
    }    
        
    public static void main(String[] args) {    
        //String fileName = "g:/temp/xwz.txt";    
        //DeleteFileUtil.deleteFile(fileName);    
//        String fileDir = "G:/temp/temp0/temp1";    
        //DeleteFileUtil.deleteDirectory(fileDir);    
        DeleteFileUtil.delete("C:\\Documents and Settings\\Administrator\\桌面\\test\\1");    
            
    }    
}   
分享到:
评论

相关推荐

    谷歌地图 delphi 封装库 2013 0.1.9 全面支持google maps api

    - Bug fixed: TGMMap -&gt; bug fixed on RemoveLinkedComponent when trying delete an object without being the list created. - Bug fixed: JavaScript =&gt; when it had figures of different types together, ...

    CryEngine DX11 全局光照+HDR展示Demo-Sponza Atrium第一部分(共两部分)

    以CryEngine DX11 全局光照+HDR展示Demo-Sponza Atrium为原型打造的3D技术Demo展示。 第一部分(共两部分) ...-Music: Kevin MacLeod ... Please don't delete,rename or remove any of blank fx file in the folder!

    CryEngine DX11 全局光照+HDR展示Demo-Sponza Atrium第二部分(共两部分)

    以CryEngine DX11 全局光照+HDR展示Demo-Sponza Atrium为原型打造的3D技术Demo展示。 第二部分(共两部分) ... ... ...-Demo was built by Wang...Please don't delete,rename or remove any of blank fx file in the folder!

    VB编程资源大全(英文源码 控制)

    1.x/6.0 (Java) &lt;END&gt;&lt;br&gt;6 , ocxex.zip&lt;br&gt;"This is a quick example I made to show you how to use Events and properties in a OCX."&lt;END&gt;&lt;br&gt;7 , news.exe&lt;br&gt;This control aids as a complete Newsgroup ...

    unigui0.83.5.820

    - UniFileUpload: File names containing Unicode chars are returned correctly - Change color of label at runtime - Various runtime property assignment bugs - DataStores and AutoDestroy - Changing ...

    ehlib_vcl_src_9_3.26

    Read about EhLib for Lazarus in the file - Lazarus&lt;*&gt;\readme.txt Overview -------- The Library contains several components and objects. TDBGridEh component TDBGridEh provides all functionality of ...

    NDK配置和开发For Android Studio

    return (*env)-&gt;NewStringUTF(env, "Hello from JNI!"); } ``` #### 三、生成 h 文件 通过`javah`命令,可以根据Java类生成对应的头文件。 **生成步骤:** 1. **运行 javah 命令**: - 使用配置好的`javah....

    EhLib 9.1.024

    Read about EhLib for Lazarus in the file - Lazarus&lt;*&gt;\readme.txt Overview -------- The Library contains several components and objects. TDBGridEh component TDBGridEh provides all functionality of ...

    ICS delphixe10源码版

    .\Samples\delphi\BroswerDemo\Resources Resource file, web pages and movie linked into browser demo .\Samples\delphi\FtpDemos Delphi Win32/Win64 FTP sample applications (all Delphi versions) .\Samples\...

    RealFlow 5.0.3 64Bit 破解

    RealFlow 5.0.3 64Bit standalone: ---------------------- ... Delete any demo license then add license and import (from file) the included license (license.txt). 4. That's all, Enjoy !!!

    文件创建读写和删除

    File filePath = new File(sdCardPath + "/your_folder_name/your_file.txt"); ``` 3. 创建文件夹(如果不存在): ```java if (!filePath.getParentFile().exists()) { filePath.getParentFile().mkdirs(); } ``` ...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + [enterprise] added "scripts" folder for additional units ("uses" directive in report script) + [enterprise] added logs for scheduler (add info in scheduler.log) + [enterprise] added property ...

    删除文件夹函数C++代码(有demo)

    在提供的Demo项目`DeleteDirectory_Win32Console_Demo`中,你可以找到这个功能的具体实现,并在Visual Studio 2008环境下编译运行。通过这个Demo,你可以学习到如何在实际项目中使用这些API函数,以及如何编写C++...

    VB编程资源大全(英文源码 网络)

    displays the number of new messages (as msn messenger display messages) and plays a WAV file&lt;END&gt;&lt;br&gt;52 , urlhist.zip&lt;br&gt;This sample demonstrates how to loop through the history folder of Internet ...

    Java邮件开发Fundamentals of the JavaMail API

    in the demo directory. Installing JavaMail 1.2 To use the JavaMail 1.2 API, download the JavaMail 1.2 implementation, unbundle the javamail-1_2.zip file, and add the mail.jar file to your ...

    VB编程资源大全(英文控件)

    that allows you to select a folder.&lt;END&gt;&lt;br&gt;87,browsfil.zip&lt;br&gt;A text box type control, that allows you to select a file..&lt;END&gt;&lt;br&gt;88,colbrwse.zip&lt;br&gt;A colour select control.&lt;END&gt;&lt;br&gt;89,colorsel.zip...

    VB编程资源大全(英文源码 控件)

    not really encryption&lt;END&gt;&lt;br&gt;21 , imagebutton2.zip&lt;br&gt;Round and freaky shape command buttons, updated from before- really an image control.&lt;END&gt;&lt;END&gt;&lt;br&gt;22 , talkeyboard.zip&lt;br&gt;Talking keyboard for ...

    cuteEditor6.0

    You can also create your own policy files that define arbitrary permission sets.&lt;br/&gt;&lt;br/&gt;Comparison of the sample security policy file &lt;br/&gt;&lt;br/&gt; &lt;br/&gt;Permissions/Resource Setting Admin Default Guest...

    计算机英语资料

    24. **Delete** - 删除 - 移除选定的内容。 25. **Undo** - 撤销 - 取消最近的操作。 26. **Redo** - 重做 - 重新执行被撤销的操作。 27. **Select** - 选择 - 选择一项或多项内容。 28. **Select All** - 全选 ...

    计算机专业英语词汇,真的很详细,java

    "Select"选择,"Select All"全选,"Find"查找,"Replace"替换,"Folder"是文件夹,"Destination Folder"是目标文件夹,"File"代表文件。"Format Diskette"是格式化磁盘。"Service Pack"是软件更新补丁,用于修复问题...

Global site tag (gtag.js) - Google Analytics