fbpx
Pages Navigation Menu

Coding is much easier than you think

How To Delete File In Java

drop table
 
The command File.delete() to delete a file in java, it will return a boolean value to indicate the delete operation status;
true if the file is deleted;
false if failed.
 

Example

 

In this example, it will delete a log file named “C:\\sample.txt”
 


package com.simplecode.file;
 
import java.io.File;
 
public class DeleteFile
{
    public static void main(String[] args)
    {	
    	try{
 
    		File file = new File("c:\sample.txt");
 
    		if(file.delete()){
    			System.out.println(file.getName() + " is deleted!");
    		}else{
    			System.out.println("Delete operation is failed.");
    		}
 
    	}catch(Exception e){
 
    		e.printStackTrace();
 
    	}
 
    }
}

About Mohaideen Jamil


I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Advertisements
%d bloggers like this: