xxxxxxxxxx
import java.io.File;
public class FileDeletionSample {
public static void main(String[] args) {
// Specify the path of the file to delete
String filePath = "path/to/file.txt";
// Register a shutdown hook to delete the file before application exit
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
// Create a new File object
File file = new File(filePath);
// Delete the file if it exists
if (file.exists()) {
file.delete();
System.out.println("File deleted successfully.");
}
}
});
// Rest of your application code
// ...
}
}