How to check if directory is empty in Java
Here’s an example to check if a directory is empty.
Favourite tutorials for developers
Here’s an example to check if a directory is empty.
In this example, the program will traverse the given directory and print out all the directories and files absolute path and name one by one.
If we use the NIO Files.delete to delete a non-empty directory in Java, it throws DirectoryNotEmptyException; for legacy IO File.delete to delete a non-empty directory, it returns a false. The standard solution is to loop the directory recursively, and delete all its children’s contents first (sub-files or sub-directories), and delete the parent later. This example shows some common ways to delete a directory in Java: (1) Files.walkFileTree + FileVisitor (Java 7), (2) Files.walk (Java 8), (3) FileUtils.deleteDirectory (Apache Common IO), (4) Recursive delete in a directory (Plain Java code).
In Java, we can use the NIO Files.createDirectory to create a directory or Files.createDirectories to create a directory including all nonexistent parent directories. For legacy IO java.io.File, the similar methods are file.mkdir() to create a directory, and file.mkdirs() to create a directory including all nonexistent parent directories. In legacy IO, the lack of exception thrown in creating directory makes developers very hard to debug or understand why we cannot create a directory, and this is one of the reasons Java releases a new java.nio.Files to throw a proper exception.
In Java, we can use the NIO createFile() to assign file permission during file creation. But, the createFile() fails to assign the group writes file permission to a file on the Unix system? To fix issues, assign file permission during file creation, first create the file, and assign its file permission later.
In Java, we can use System.getProperty(“java.io.tmpdir”) to get the default temporary file location. For Windows, the default temporary folder is %USER%\AppData\Local\Temp. For Linux, the default temporary folder is /tmp . Alternatively, we can create a temporary file and substring the file path to get the temporary file location.
In Java, we can use the NIO Files.delete() or Files.deleteIfExists() to delete a temporary file, it works the same like delete a regular text file. We also can uses the legacy IO java.io to create a temporary file, and delete it later. The legacy IO has some drawbacks, if possible, always picks the new Java NIO java.nio.* for file IO stuff.
The temporary file is just a regular file created on a predefined directory. In Java, we can use the NIO Files.write() to write data to a temporary file. In Java, there are many ways to write data or text to a temporary file; it works like writing to a regular file. You can choose BufferedWriter, FileWriter etc, but in most cases, the NIO java.nio.Files should be enough to write or append data to a file.
In Java, we can use the Java NIO Files.createTempFile() methods to create a temporary file in the default temporary-file directory. The default temporary file folder is vary on operating system: Windows – %USER%\AppData\Local\Temp, Linux – /tmp . We can use the legacy Java IO java.io.* to create a temporary file, it still works perfectly, but the new Java NIO should always be the first choice.
In previous article, we show how to compress a file in Gzip format. This article shows how to decompress a Gzip file. We copy the GZIPInputStream into a FileOutputStream to decompress a Gzip file. Instead, we can use NIO File.copy to copy a file. Also we can decompress a Gzip file into a byte[] directly without saving it into a local file.