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:
Thanks. Its useful. Can I find this File api in Groovy api ? if so please share details.
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
Post a Comment