Java Program To Delete a File -
import java.io.*;
public class delete
{
public static void main(String[] args)
{
File file = new File("D:\\Coding57\\Java\\Coding.txt");
if(file.delete())
{
System.out.println("File deleted successfully");
}
else
{
System.out.println("Failed to delete the file");
}
}
}
Output -
File deleted successfully
How this program works?
In this program we have made an object of the File and in that we have declared the directory of the file that is to be deleted. Then we have used the file.delete() method to delete the specific file in the directory, as here if else is used to check whether the file is located on the given directory or not if it is then it will print "File deleted successfully" if it is not then it will print "Failed to delete the file".
0 Comments