Description:
This java tutorial provide example source code in deleting a file or directory. This includes basic and complex strategies in how to delete a file or a directory in Java.
Simple Java program to delete a file
This sample source code shows how to delete a file in java illustrating the delete() method in File class. The File class under java.io package is an abstract representation of physical computer file and pathnames. By calling the delete method, you are instructing the java program to delete the file or directoryspecified by calling the File constructor.
package com.javatutorialhq.tutorial; import java.io.File; public class SimpleDeleteFile { /** * This simple java sample code shows how delete a file in java illustrating * basic error trapping * Property of javatutorialhq.com * All Rights Reserved * Version 1.0 06/11/2012 */ public static void main(String[] args) { String fileInput = "C:\\temp\\Delete File sample.txt"; SimpleDeleteFile deleteClass = new SimpleDeleteFile(); deleteClass.invokeDelete(fileInput); } private boolean invokeDelete(String fileName) { File file = new File(fileName); if (file.exists()) { System.out.println("File exists, preparing to delete"); boolean result = file.delete(); // test if delete of file is success or not if (result) { System.out.println("File " + fileName + " deleted"); } else { System.out.println("File was not deleted, unknown reason"); } return result; } else { System.out.println("File delete failed, file does not exists"); return false; } } }
Sample output:
File exists, preparing to delete File C:\temp\Delete File sample.txt deleted
Running it again will result to
File delete failed, file does not exists
Running this java sample program will delete the file with filename declared on the File constructor. However running it again will flag a file does not exists message because of the checker that we put through by invoking the exists() method of the File class. We have also included a checker if deletion is success or not other than the existence of the file. Possible reason for the delete to fail is due to lock or access denied condition.
Delete a folder in Java
This succeeding example shows how to delete a directory in java. As stated earlier the File class is a representation of file and pathnames. So basically we can use the same technique similar to deleting a file. However we need to put a conditional statement to check wether the directory is empty or not. Java will not allow to delete a directory which is not empty. The list method on File class return an array of directories and files on a given directory. We would use this method to check if the directory is ready to be deleted or not.
package com.javatutorialhq.tutorial; import java.io.File; public class DeleteFolder { /** * This sample code shows how delete a file and folder in java * illustrating basic error trapping on filde deletion * Property of javatutorialhq.com * All Rights Reserved * Version 1.0 06/11/2012 */ public static void main(String[] args) { String fileInput = "C:\\temp\\sample folder"; DeleteFolder deleteClass = new DeleteFolder(); deleteClass.invokeDelete(fileInput); } private boolean invokeDelete(String fileName) { File file = new File(fileName); if (file.exists()) { if (file.isDirectory()) { if ((file.list()).length > 0) { System.out.println("Folder not empty"); return false; } } System.out.println("File exists, preparing to delete"); boolean result = file.delete(); // test if delete of file is success or not if (result) { System.out.println("File " + fileName + " deleted"); } else { System.out.println("File was not deleted, unknown reas/n"); } return result; } else { System.out.println("File delete failed, file does not exists"); return false; } } }
The example code to delete a folder in java as illustrated above includes as well file deletion. However one limitation of this example is the deletion of files inside the folder. Our next example illustrates how to solve this.
Delete a folder in Java recursively – all files and directory
As previous example stated, the delete function only allow us to delete an empty folder using Java. However, since the delete method can delete only folder that is empty there is a need to construct another code logic to delete the folder recursively. We will be using the list() method of File class to do the deletion recursively.
package com.teknoscope.tutorial; import java.io.File; public class RecursiveDelete { /** * This java sample code shows * how to delete a file or folder recursively * with basic error trapping * Property of teknoscope.com * All Rights Reserved * Version 1.0 06/11/2012 */ public static void main(String[] args) { String inputFilename = "C:\\temp\\samplefolder"; RecursiveDelete recursiveDelete = new RecursiveDelete(); if(recursiveDelete.invokeDelete(inputFilename)){ System.out.println("Delete completed"); } else{ System.out.println("File not deleted, unknow reason"); } } private boolean invokeDelete(String fileName) { File file = new File(fileName); if (file.exists()) { //check if the file is a directory if (file.isDirectory()) { if ((file.list()).length > 0) { for(String s:file.list()){ //call deletion of file individually (new RecursiveDelete()).invokeDelete(fileName+"\\"+s); } } } boolean result = file.delete(); // test if delete of file is success or not if (result) { System.out.println("File " + fileName + " deleted"); } else { System.out.println("File was not deleted, unknown reason"); } return result; } else { System.out.println("File delete failed, file does not exists"); return false; } } }
Sample output:
File C:\temp\samplefolder\java delete file bitmap.bmp deleted File C:\temp\samplefolder\java delete folder\2.accdb deleted File C:\temp\samplefolder\java delete folder\New Bitmap Image.bmp deleted File C:\temp\samplefolder\java delete folder\next folder\4\logs deleted File C:\temp\samplefolder\java delete folder\next folder\4 deleted File C:\temp\samplefolder\java delete folder\next folder\New WinRAR ZIP archive.zip deleted File C:\temp\samplefolder\java delete folder\next folder deleted File C:\temp\samplefolder\folder deleted File C:\temp\samplefolder\java delete rar file.zip deleted File C:\temp\samplefolder\sample folder 2 deleted File C:\temp\samplefolder deleted Delete completed
File validation is still intact on this example program as inherited from previous two example.[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]