java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to delete a file?



public class FileDeleteExample
    public static void main(String[] args)
        // When want to delete a file named readme.txt
        File file = new File("readme.txt");
        // Checks if the file is exists before deletion.
        if (file.exists())
            System.out.println("Deleting file " + file.getAbsolutePath());
            // Use the delete method to delete the given file.
        } else
            System.out.println(file.getAbsolutePath() + " doesn't exists.");