Java program to delete file in Java
hptts://www.theitroad.com
Here's a Java program to delete a file:
import java.io.File; public class Main { public static void main(String[] args) { File file = new File("path/to/file"); if (file.delete()) { System.out.println("File deleted successfully"); } else { System.out.println("Failed to delete file"); } } }
This program uses the File
class to represent the file we want to delete. We then call the delete()
method on this object to delete the file. If the file is successfully deleted, the program will print "File deleted successfully" to the console. Otherwise, it will print "Failed to delete file".
Note that this program will only work for files. If you want to delete a directory, you will need to use a different method.