Facebook PixelFile Deletion | Java Tutorial | CodeWithHarry

File Deletion

Deleting files is easy in Java. You just need to 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 Successfully: " + file.getName());
        } else {
            System.out.println("Error......");
        } 
    }
}

Output:

Deleted Successfully: students.txt