How To Delete Directory In Java
来源:互联网 发布:开发抢票软件 编辑:程序博客网 时间:2024/05/21 16:56
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.phpFile is deleted : C:\mkyong-new\archive.php...Directory is deleted : C:\mkyong-new\includesFile is deleted : C:\mkyong-new\index.phpFile is deleted : C:\mkyong-new\index.php.hackedFile is deleted : C:\mkyong-new\js\hoverIntent.jsFile is deleted : C:\mkyong-new\js\jquery-1.4.2.min.jsFile is deleted : C:\mkyong-new\js\jquery.bgiframe.min.jsDirectory is deleted : C:\mkyong-new\js\superfish-1.4.8\cssDirectory is deleted : C:\mkyong-new\js\superfish-1.4.8\imagesDirectory is deleted : C:\mkyong-new\js\superfish-1.4.8File is deleted : C:\mkyong-new\js\superfish-navbar.css...Directory is deleted : C:\mkyong-newDone
0 0
- How To Delete Directory In Java
- How to Delete [This is no longer located in Directory] Files
- How to enable directory listing in tomcat
- How to delete pending changelist in perforce
- How To Create Directory In Java--创建单个目录,多个目录(子目录)
- How to Copy an Entire Directory in VB6?
- How to Setup X500 Environment in Sun ONE Directory
- How to version control /etc directory in Linux
- How to change mysql default data directory in Ubuntu
- how to get cwd(current working directory) in python?
- How to Change default MySQL Data Directory in Linux111
- How to access local directory with script executed in oozie?
- how to unzip a file in a system directory
- How to Confirm a Delete in an ASP.NET Datagrid...
- how to delete "permission from trustedinstaller" folder in Win7/Vista
- How to Delete Process Chain Logs in SAP BW?
- How to delete duplicate rows without children in sql server
- How to Delete a Business Partner in SAP CRM
- sql 语句: COLLATE Chinese_PRC_CI_AS什么意思?
- Hive Shell 基本操作
- 孤儿和僵尸进程
- cocos2dx 独立项目遇到的问题
- webservice 开发小解
- How To Delete Directory In Java
- Linux常用命令大全
- win7 64位
- linux的chmod,chown命令 详解
- SharePoint中的权限体系
- hive 优化
- Unity3D 设置GameObjet 的隐藏和显示
- php包含那点事情
- 敏捷思维学习Ext.Net MVC--3.3文本字段与后台的高级通信(model)