Delete File using Java API

Delete file using java with the help of delete method available in java.io.File package. The File.delete() method return boolean value. if the file is deleted successfully then the delete() method return true, on the other hand if it not able to delete file using java then it returns false.

below example demonstrates how to delete file using java delete() method
import java.io.File;

	public class DeleteFile{
		
	    public static void main(String args[]) {
	      try{
	    	
	    	File file = new File("c:\\testfile.txt");
	    	
	    	if(file.delete()){
	    		System.out.println("file "+file.getName()+" deleted  successfully");
	    	}
	    	else{
	    		System.out.println("unable to delete the file "+file.getName());
	    	}
	 
	    }
	      catch(Exception ex){
	    	  ex.printStackTrace();
	      }
	   }
	}

output:-

file testfile.txt deleted successfully

java.nio.file.Files provides delete(Path) method which delete file using java and can throw an exception such as NoSuchFileException, if the file does not exist.

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.NoSuchFileException;

	public class DeleteFile{
		
	    public static void main(String args[]) {
	    	
              Path path = null;
	      try{
	    	
	    	File file = new File("c:\\testfile.txt");
	    	
	    	path = file.toPath();
	    	
	    	Files.delete(path);
	    	
	    	if(!file.exists()){
	    		System.out.println("file "+file.getName()+" deleted successfully");
	    	 }
	    	else{
	    		System.out.println("unable to delete the file "+file.getName());
	    	}
	 
	    }
              catch (NoSuchFileException nex) {
                  System.err.format("%s: no such" + " file or directory%n", path);
              }
	      catch(Exception ex){
	    	  ex.printStackTrace();
	      }
	   }
	}

output:-

file testfile.txt deleted successfully

java.nio.file.Files also provides another method deleteIfExists(Path) method. as name suggests this method does not throw exception if the file does not exist. The method return boolean value true if delete file using java successfully or false, if not able to delete the file or file does not exist.

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;

	public class DeleteFile{
		
	    public static void main(String args[]) {
	    	
	      try{
	    	
	    	File file = new File("c:\\testfile.txt");
	    	
	    	Path path = file.toPath();
	    	
	    	boolean status = Files.deleteIfExists(path);
	    	
	    	if(status == true){
	    		System.out.println("file "+file.getName()+" deleted successfully");
	    	 }
	    	else{
	    		System.out.println("unable to delete the file "+file.getName());
	    	}
	 
	    }
	      catch(Exception ex){
	    	  ex.printStackTrace();
	      }
	   }
	}

output:-
delete file using java

There are various other API’s available to delete the file like apache commons FileUtils. using FileUtils we can delete the file recursively as well as delete the file force fully.

Leave a Comment