Delete File in Java

Exercise:

Write a Java Program to delete file.

1.Delete using delete()

Click Here to View the Solution!
import java.io.File;

class Main {
    public static void main(String[] args) {

        // creates a file object
        File file = new File("JavaFile.java");

        // deletes the file
        boolean value = file.delete();
        if(value) {
            System.out.println("JavaFile.java is successfully deleted.");
        }
        else {
            System.out.println("File doesn't exit");
        }
    }
}
Click Here to View the Output!
if the file is present, then the message JavaFile.java is successfully deleted is shown. Otherwise, File doesn't exit is shown.
Click Here to View the Explanation!
  • This program deals with the deletion process of a given file.
  • For this purpose first import File class into the program.
  • Then in the main method, instantiate a new File class object, and pass the file name into its constructor.
  • The file name consists of a program file having java extension.
  • For the deletion purpose, a method of File class is called upon the File type object.
  • This method is named delete().
  • The return value of this method is of boolean type, therefore is stored into a boolean type variable.
  • The if-condition checks whether the file exists or not, if so is the case then the file is deleted.
  •  In other case, a message is displayed indicating that file does not exist.

2.Delete using deleteIfExists()

Click Here to View the Solution!
import java.io.File;

class Main {
    public static void main(String[] args) {

        // creates a file object
        File file = new File("JavaFile.java");

        // deletes the file
        boolean value = file.deleteIfExists();
        if(value) {
            System.out.println("JavaFile.java is successfully deleted.");
        }
        else {
            System.out.println("File doesn't exit");
        }
    }
}
Click Here to View the Output!
if the file is present, then the message JavaFile.java is successfully deleted is shown. Otherwise, File doesn't exit is shown.
Click Here to View the Explanation!
  • This program introduces another method to deal with the deletion process of a given file.
  • A new library of java is utilized to work with file-management process.
  • The new method, and library is: java.nio & deleteIfExists() respectively.
  • A specialty of java.nio is that it deals with buffers in comparison with java.io which works with streams.
  • First step of execution involves, importing Path, Paths, and File classes in the program.
  • In the main method, instantiate a Path type object with the help of a static method named get(); which belongs to Paths class.
  • This object will constitute the name of the file along with the path of the directory where the file is stored.
  • This obtained path is passed as an argument to File class method i.e. deleteIfExists().
  • The return value of the aforementioned method is of boolean type.
  • If the file exists then it will be deleted.
  • Otherwise, a message is displayed indicating that the file does not exist.

%d bloggers like this: