This example illustrates how to delete the specified file or directory after checking weather the file exists or not.
This example illustrates how to delete the specified file or directory after checking weather the file exists or not.This example illustrates how to delete the specified
file or directory after checking weather the file exists or not. This topic is related to the
I/O (input/output) of java.io
package.
In this example we are using File class of java.io package. The File class is an abstract representation of file and directory pathnames.
Explanation
This program deletes the specified file if that exists. We will be declaring a function called deletefile which deletes the specified directory or file.
deletefile(String file)
The function deletefile(String file) takes file name as parameter. The function creates a new File instance for the file name passed as parameter
File f1 = new File(file);
and delete the file using delete function f1.delete();
which return the Boolean value (true/false). It returns true
if and only if the file or directory is successfully deleted; false
otherwise.
delete()
true
if and only if the file or directory is successfully
deleted; false
otherwise
Code of the Program :
import java.io.*;
|
Download File Deletion Example
Ads