how to delete a file in java file handling code example

Example 1: java delete file

// java delete file
import java.io.File;
public class JavaDeleteFile
{
   public static void main(String[] args) 
   {
      try
      {         
         File file = new File("E:\\example.txt");  
         if(file.delete())  
         {
            System.out.println(file.getName() + " file deleted.");  
         }
         else  
         {  
            System.out.println("failed");  
         }
      }  
      catch(Exception ex)  
      {  
         ex.printStackTrace();  
      }
   }
}

Example 2: how to delete a file in java

File file = new File("filepath");
file.delete(); //Returns boolean

Tags:

Java Example