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…
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…
In Java, we can use Files.exists(path) to test whether a file exists. The path can be a file or a directory. It is better to combine with !Files.isDirectory(path) to ensure…
In Java, we can use System.getProperty("user.dir") to get the current working directory, the directory from where your program was launched. The article shows different ways like File, Paths, FileSystems, or…