Posted in: Core Java
import java.io.*;
public class DeleteFileOrDirectory {
public static void main(String[] args) {
//create file object
File file = new File("C://FileIO/DeleteDemo.txt");
/*
* To delete a file or directory from filesystem, use
* boolean delete() method of File class.
*
* This method returns true if file or directory successfully deleted. If
* the file is a directory, it must be empty.
*/
boolean blnDeleted = file.delete();
System.out.println("Was file deleted ? : " + blnDeleted);
/*
* Please note that delete method returns false if the file did not exists or
* the directory was not empty.
*/
}
}
/*
Output would be
Was file deleted ? : true
*/
Email This
BlogThis!
Share to Facebook