Here’s an example to check if a directory is empty.
Example
packagecom.mkyong.file;importjava.io.File;publicclass CheckEmptyDirectoryExample
{publicstaticvoid main(String[] args){File file =newFile("C:\\folder");if(file.isDirectory()){if(file.list().length>0){System.out.println("Directory is not empty!");}else{System.out.println("Directory is empty!");}}else{System.out.println("This is not a directory");}}}
Here’s an example to check if a directory is empty.
Example
packagecom.mkyong.file;importjava.io.File;publicclass CheckEmptyDirectoryExample
{publicstaticvoid main(String[] args){File file =newFile("C:\\folder");if(file.isDirectory()){if(file.list().length>0){System.out.println("Directory is not empty!");}else{System.out.println("Directory is empty!");}}else{System.out.println("This is not a directory");}}}
Here’s an example to copy a directory and all its sub-directories and
files to a new destination directory. The code is full of comments and
self-explanatory, left me comment if you need more explanation.
Example
Copy folder “c:\\mkyong” and its sub-directories and files to another new folder “c:\\mkyong-new“.
packagecom.mkyong.file;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;publicclass CopyDirectoryExample
{publicstaticvoid main(String[] args){File srcFolder =newFile("c:\\mkyong");File destFolder =newFile("c:\\mkyong-new");//make sure source existsif(!srcFolder.exists()){System.out.println("Directory does not exist.");//just exitSystem.exit(0);}else{try{
copyFolder(srcFolder,destFolder);}catch(IOException e){
e.printStackTrace();//error, just exitSystem.exit(0);}}System.out.println("Done");}publicstaticvoid copyFolder(File src, File dest)throwsIOException{if(src.isDirectory()){//if directory not exists, create itif(!dest.exists()){
dest.mkdir();System.out.println("Directory copied from "+ src +" to "+ dest);}//list all the directory contentsString files[]= src.list();for(String file : files){//construct the src and dest file structureFile srcFile =newFile(src, file);File destFile =newFile(dest, file);//recursive copy
copyFolder(srcFile,destFile);}}else{//if file, then copy it//Use bytes stream to support all file typesInputStream in =newFileInputStream(src);OutputStream out =newFileOutputStream(dest);byte[] buffer =newbyte[1024];int length;//copy the file content in bytes while((length = in.read(buffer))>0){
out.write(buffer, 0, length);}
in.close();
out.close();System.out.println("File copied from "+ src +" to "+ dest);}}}
Result
Directory copied from c:\mkyong to c:\mkyong-new
File copied from c:\mkyong\404.php to c:\mkyong-new\404.php
File copied from c:\mkyong\footer.php to c:\mkyong-new\footer.php
File copied from c:\mkyong\js\superfish.css to c:\mkyong-new\js\superfish.css
File copied from c:\mkyong\js\superfish.js to c:\mkyong-new\js\superfish.js
File copied from c:\mkyong\js\supersubs.js to c:\mkyong-new\js\supersubs.js
Directory copied from c:\mkyong\images to c:\mkyong-new\images
File copied from c:\mkyong\page.php to c:\mkyong-new\page.php
Directory copied from c:\mkyong\psd to c:\mkyong-new\psd
...
Done
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.
packagecom.mkyong.file;importjava.io.File;importjava.io.IOException;publicclass DeleteDirectoryExample
{privatestaticfinalString SRC_FOLDER ="C:\\mkyong-new";publicstaticvoid main(String[] args){File directory =newFile(SRC_FOLDER);//make sure directory existsif(!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");}publicstaticvoid delete(File file)throwsIOException{if(file.isDirectory()){//directory is empty, then delete itif(file.list().length==0){
file.delete();System.out.println("Directory is deleted : "+ file.getAbsolutePath());}else{//list all the directory contentsString files[]= file.list();for(String temp : files){//construct the file structureFile fileDelete =newFile(file, temp);//recursive delete
delete(fileDelete);}//check the directory again, if empty then delete itif(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