📜  用于检查目录是否为空的Java程序

📅  最后修改于: 2022-05-13 01:54:28.775000             🧑  作者: Mango

用于检查目录是否为空的Java程序

Java.io.File中定义的list() 方法用于获取存在于由其路径名定义的指定目录中的文件和文件夹(目录)列表。文件列表存储在字符串数组中。如果数组的长度大于0,则指定的目录不为空,否则为空。

方法一:使用list()方法

  • 假设路径 /home/user/folder 中有一个目录,它包含三个文本文件。
  • 使用 list() 方法创建一个数组并存储文件名。
  • 计算字符串数组的长度
  • 打印结果

有关上述方法,请参阅以下程序。

Java
// Java Program to check if 
// a directory is empty or not
  
import java.io.File;
  
class GFG {
    
    public static void main(String[] args)
    {
        // mention the directory path
        File directory = new File("/home/mayur");
  
        if (directory.isDirectory()) {
            
            // creating a String Array
            // store name of files
            String arr[] = directory.list();
  
            // check if length is greater than 0 or not
            if (arr.length > 0) {
                System.out.println("The directory "
                                   + directory.getPath()
                                   + " is not Empty!");
            }
            else {
                System.out.println("The directory "
                                   + directory.getPath()
                                   + " is Empty!");
            }
        }
    }
}


Java
// Java Program to check if a
// directory is empty or not
  
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 {
    
    // boolean function which
    // return true if directory is
    // empty else return false
    public static boolean isEmptyDirectory(File directory)
        throws IOException
    {
        // check if given path is a directory
        if (directory.exists()) {
            if (!directory.isDirectory()) {
  
                // throw exception if given path is a
                // file
                throw new IllegalArgumentException(
                    "Expected directory, but was file: "
                    + directory);
            }
            else {
                // create a stream and check for files
                try (DirectoryStream directoryStream
                     = Files.newDirectoryStream(
                         directory.toPath())) {
                    // return false if there is a file
                    return !directoryStream.iterator()
                                .hasNext();
                }
            }
        }
        // return true if no file is present
        return true;
    }
  
    public static void main(String[] args)
        throws IOException
    {
        // enter path of directory
        File directory = new File("/home/mayur");
  
        // call isEmptyDirectory Function
        if (isEmptyDirectory(directory)) {
            System.out.println("The directory "
                               + directory.getPath()
                               + " is Empty!");
        }
        else {
            System.out.println("The directory "
                               + directory.getPath()
                               + " is Not Empty!");
        }
    }
}


输出:

方法二:使用DirectoryStream

从Java 7 开始,引入了 Files.newDirectoryStream 方法,该方法返回 DirectoryStream 以迭代目录中的所有条目。

  • 假设路径 /home/user/folder 中有一个目录,它包含三个文本文件。
  • 创建一个布尔函数来检查目录是否为空
  • 如果给定的路径是文件,则抛出异常
  • 如果给定目录有文件,则返回 false 否则返回 true。
  • 打印结果

有关上述方法,请参阅以下程序。

Java

// Java Program to check if a
// directory is empty or not
  
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 {
    
    // boolean function which
    // return true if directory is
    // empty else return false
    public static boolean isEmptyDirectory(File directory)
        throws IOException
    {
        // check if given path is a directory
        if (directory.exists()) {
            if (!directory.isDirectory()) {
  
                // throw exception if given path is a
                // file
                throw new IllegalArgumentException(
                    "Expected directory, but was file: "
                    + directory);
            }
            else {
                // create a stream and check for files
                try (DirectoryStream directoryStream
                     = Files.newDirectoryStream(
                         directory.toPath())) {
                    // return false if there is a file
                    return !directoryStream.iterator()
                                .hasNext();
                }
            }
        }
        // return true if no file is present
        return true;
    }
  
    public static void main(String[] args)
        throws IOException
    {
        // enter path of directory
        File directory = new File("/home/mayur");
  
        // call isEmptyDirectory Function
        if (isEmptyDirectory(directory)) {
            System.out.println("The directory "
                               + directory.getPath()
                               + " is Empty!");
        }
        else {
            System.out.println("The directory "
                               + directory.getPath()
                               + " is Not Empty!");
        }
    }
}

输出: