Friday 18, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 24 May 2017

How to delete directory using Java

boolean java.io.File.delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Note that the java.nio.file.Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.

Returns:
true if and only if the file or directory is successfully deleted; false otherwise

Throws:
SecurityException - If a security manager exists and its java.lang.SecurityManager.checkDelete method denies delete access to the file

DeleteDirectory.java
package com.javatutorialcorner.directory;

import java.io.File;

public class DeleteDirectory {

 public static void main(String[] args) {
  File directory = new File("C:/JTC/");
  boolean isDeleted =  directory.delete();
  System.out.println("Is Directory Deleted "+isDeleted);
  isDeleted =  deleteDirectory(directory);
  System.out.println("Is Directory Deleted "+isDeleted);
  
 }

  public static boolean deleteDirectory(File dir) {
       if (dir.isDirectory()) {
          String[] children = dir.list();
          for (int i = 0; i < children.length; i++) {
             boolean success = deleteDirectory (new File(dir, children[i]));
             
             if (!success) {
                return false;
             }
          }
       }
       return dir.delete();
    }
}
Output
Is Directory Deleted false
Is Directory Deleted true

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to create temp file using Java Following example shows how to create temp file using file.createTempFile() method in Java. createTempFile public static File createTempFile(String prefix,String suffix) throws IOException Creates an empty file in the defaul… Read More
  • How to make read-only file using Java Following example shows how to make file read-only using file.setReadOnly() method in Java. setReadOnlypublic boolean setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations a… Read More
  • How to get File Last modified Date in Java Following example shows how to get file last modified date using file.lastModified() method in Java. lastModified public long lastModified()  Returns the time that the file denoted by this abstract pathname was last mo… Read More
  • How to compare paths of two file using Java Following example shows how to compare paths of two file using file1.compareTo() method in Java. compareTo public int compareTo(File pathname)  Compares two abstract pathnames lexicographically. The ordering defined by… Read More
  • How to delete file using Java Following example shows how to delete file using file.delete() method in Java. delete public boolean delete() Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to delete directory using Java Rating: 5 Reviewed By: eHowToNow