Recursively Delete Directories in Java

In Java, a readable, writable directory should be empty before an attempt is made to remove it. The following code recursively empties a directory, and its sub-directories, and then removes them.

 private void deleteFile(File p_file) { String FILE_SEPARATOR = System.getProperty("file.separator"); // If it is a directory, empty it first System.out.println("Deleting: "+p_file.getPath()); if(p_file.isDirectory()) { String[] dirList = p_file.list(); for(int i=0; i

Note that a File object should be writable by the current user of the code in order to be removed.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: