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 :
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.
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
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 lawn cotton suits , pakistani lawn suits for sale , pakistani lawn suits online shopping , lawn suits online , pakistani lawn suits price , pakistani lawn dress , pakistani cotton suits , designer lawn suits online , pakistani lawn suits wholesale , lawn suits sale
ReplyDeletehermes belts for men
ReplyDeletekyrie 4 shoes
nike cortez
off white shoes
kyrie irving shoes
kyrie 6
balenciaga
michael kors outlet
yeezy boost 350 v2
cheap jordans
link check that click this site view it now find this Learn More
ReplyDeleter9o36m4l51 g6w89m2d01 e2p87y9x56 k4d53r8y79 z2l26x7w59 u8c12i2d42
ReplyDelete