Thursday, September 24, 2009

Groovy JDK (GDK): File.deleteDir()

One of the many things that I like about Groovy is its Groovy JDK (also known as GDK). As the Groovy JDK API Specification states, the Groovy JDK provides "methods added to the JDK to make it more groovy." The Groovy JDK is a collection of extensions to classes available in the standard Java SDK such as Object. As a collection of extensions to the standard Java JDK, the Groovy JDK is separate and distinct from the Groovy API, which consists of classes unique to Groovy. In this blog post, I focus on one highly useful method the Groovy JDK adds to the standard java.io.File class for deleting a directory.

The standard Java JDK provides a File.delete() method that will delete files or directories. The Javadoc documentation for this method explicitly states that this method will not delete a directory that is not empty (contains other directories or files). The same Javadoc documentation also states that the boolean returned by this method will be true if and only if the directory is actually deleted. In other words, if the directory is not deleted because it does not exist or because it contains files or other directories, a false will be returned.

The Groovy JDK (GDK) provides an extension to the standard File class. The GDK File extension adds a method deleteDir() that behaves differently on directory deletion than the just-described behavior of the standard File.delete() method. In particular, the GDK File.deleteDir() will delete a directory even if it contains other files and directories (it deletes those contents as well, of course). This is kind of like the rm -rf in Linux and is particularly useful when one is certain he or she really wants to delete the directory no matter its contents. This is often the case when writing scripts.

Besides obediently removing a directory regardless of it containing files and other directories, the GDK File.deleteDir() method is also different from the standard File.delete() when acting on directories because it returns true both in situations in which a directory is successfully deleted and in situations in which the specified directory does not exist. On the other hand, false is returned when the directory does exist and cannot be deleted or when the File being acted upon is not actually a directory.

The following Groovy code can be used to illustrate the differences between the standard SDK File.delete() and the GDK File.deleteDir().

directoryDelete.groovy

directory = new File("dirToBeDeleted")
printf "Delete directory $directory with JDK File.delete(): ${directory.delete()}%n"
printf "Delete directory $directory with GDK File.deleteDir(): ${directory.deleteDir()}"


The next screen snapshot shows the results of executing the above Groovy code snippet. The snapshot shows that the "dirToBeDeleted" directory does exist and that it does have three .txt files in it.



The first attempt to delete this directory with files using File.delete() fails and returns false. However, the second attempt with the GDK File.deleteDir() is successful.

The next screen snapshot shows how both the standard File.delete() returns true when called on to remove an empty directory. Even with the directory now removed, the GDK File.deleteDir() also returns true.



Finally, we can see an example of when the Groovy JDK File.deleteDir() returns false. To make this happen, I changed the permissions of the "dirToBeDeleted" directory to not allow reading or writing. When I run the Groovy script against that locked-down directory, I see the following results:



The Groovy JDK (GDK) File.deleteDir() method is a useful extension that can make for easier and more convenient scripting that involves deletion of directories. This is just one of many "extension" methods that the GDK provides as enhancements to the functionality provided by the standard JDK.

2 comments:

Raaz said...

Thanks. Its useful. Can I find this File api in Groovy api ? if so please share details.

@DustinMarx said...

Raaz,

You can find Groovy GDK's File API documented at http://docs.groovy-lang.org/latest/html/groovy-jdk/java/io/File.html.

Dustin