File Deletion

 

File Deletion

Deleting files is easy in java, just write an if…..else block and use the delete() method inside it.

Example:

import java.io.File;

public class DeleteFile {
    public static void main(String[] args) {
        File file = new File("students.txt"); 
        if (file.delete()) { 
            System.out.println("Deleted Sccessfully: " + file.getName());
        } else {
            System.out.println("Error......");
        } 
    }
}


Output:

Deleted Sccessfully: students.txt