Delete file or directory


  1. import java.io.*;
  2. public class DeleteFileOrDirectory {
  3. public static void main(String[] args) {
  4. //create file object
  5. File file = new File("C://FileIO/DeleteDemo.txt");
  6. /*
  7.   * To delete a file or directory from filesystem, use
  8.   * boolean delete() method of File class.
  9.   *
  10.   * This method returns true if file or directory successfully deleted. If
  11.   * the file is a directory, it must be empty.
  12.   */
  13. boolean blnDeleted = file.delete();
  14. System.out.println("Was file deleted ? : " + blnDeleted);
  15. /*
  16.   * Please note that delete method returns false if the file did not exists or
  17.   * the directory was not empty.
  18.   */
  19. }
  20. }
  21. /*
  22. Output would be
  23. Was file deleted ? : true
  24. */

Attended Any Interviews ? Please send us Questions to faqs@javastuff.in