import
java.io.File;
import
java.io.IOException;
import
java.nio.file.DirectoryStream;
import
java.nio.file.Files;
import
java.nio.file.Path;
public
class
GFG {
public
static
boolean
isEmptyDirectory(File directory)
throws
IOException
{
if
(directory.exists()) {
if
(!directory.isDirectory()) {
throw
new
IllegalArgumentException(
"Expected directory, but was file: "
+ directory);
}
else
{
try
(DirectoryStream<Path> directoryStream
= Files.newDirectoryStream(
directory.toPath())) {
return
!directoryStream.iterator()
.hasNext();
}
}
}
return
true
;
}
public
static
void
main(String[] args)
throws
IOException
{
File directory =
new
File(
"/home/mayur"
);
if
(isEmptyDirectory(directory)) {
System.out.println(
"The directory "
+ directory.getPath()
+
" is Empty!"
);
}
else
{
System.out.println(
"The directory "
+ directory.getPath()
+
" is Not Empty!"
);
}
}
}