Delete
Lesson 7 of 11 in Coddy's File Handling in Java course.
Lastly, to delete a file in Java, use the File class method delete(). The method will return true if the file was deleted successfully, or false if failed:
File file = new File("data.txt");
if (file.delete()) {
System.out.println("Deleted the file!");
} else {
System.out.println("Failed to delete the file.");
}The above code attempts to delete the file named "data.txt".
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Try it yourself
This lesson doesn't include a code challenge.