Deleting a File in java

Java Programming Java8Java.IO Package

You can delete a file using the delete() method of the File class.

Program

Live Demo

import java.io.File;

public class DeleteFileExample {
   public static void main(String[] args) {
      try {
         File file = new File("myFile");
         if(file.delete()) {
            System.out.println(file.getName() + " is deleted!");
         } else {
            System.out.println("Delete operation is failed.");
         }
         catch(Exception e) {
            e.printStackTrace();
      }
   }
}   

Output

Delete operation is failed.
raja
Published on 04-Jun-2018 12:55:10
Advertisements