Introduction
In this lab, we will learn how to delete a file or directory in Java using the File
class. We will cover the delete()
and deleteOnExit()
methods, and provide examples of how to use them.
In this lab, we will learn how to delete a file or directory in Java using the File
class. We will cover the delete()
and deleteOnExit()
methods, and provide examples of how to use them.
The first thing we need to do is to import the File
class which we will use to delete a file or directory. Add the following code at the beginning of your DeleteFile.java
file:
import java.io.File;
To delete a file using Java's File
class, we can use the delete()
method. This method accepts no arguments and returns a boolean value indicating whether or not the file was deleted. If the file is successfully deleted, the method will return true
. Otherwise, it will return false
. Add the following code to your DeleteFile.java
file:
File file = new File("filename.txt");
boolean delete = file.delete();
if(delete) {
System.out.println("File deleted successfully.");
} else {
System.out.println("Failed to delete file!");
}
Note: Replace filename.txt
with the name of the file you want to delete.
To run the code, navigate to the directory where your DeleteFile.java
file is stored in the terminal and execute the following commands:
javac DeleteFile.java
java DeleteFile
To delete a directory using the delete()
method in Java, the directory must be empty and can't contain any other files or sub-directories. If it contains any files or sub-directories then the method will not be successful. Add the following code to your DeleteFile.java
file:
File directory = new File("directoryName");
boolean delete = directory.delete();
if(delete) {
System.out.println("Directory deleted successfully.");
} else {
System.out.println("Failed to delete directory!");
}
Note: Replace directoryName
with the name of the directory you want to delete.
To run the code, navigate to the directory where your DeleteFile.java
file is stored in the terminal and execute the following commands:
javac DeleteFile.java
java DeleteFile
To delete a file using the deleteOnExit()
method, we call the method on the file object. This method will delete the file when the virtual machine terminates. It returns void value, which means it does not return anything. Add the following code to your DeleteFile.java
file:
File file = new File("filename.txt");
file.deleteOnExit();
System.out.println("File will be deleted when the program exits.");
Note: Replace filename.txt
with the name of the file you want to delete on exit.
To run the code, navigate to the directory where your DeleteFile.java
file is stored in the terminal and execute the following commands:
javac DeleteFile.java
java DeleteFile
In this lab, we learned how to delete a file or directory using the File
class in Java. We covered the delete()
and deleteOnExit()
methods and provided examples of how to use them. With these methods, you can easily delete files and directories as per your convenience.