【问题标题】:Java: How to read directory folder, count and display no of files and copy to another folder?Java:如何读取目录文件夹,计算和显示文件数量并复制到另一个文件夹?
【发布时间】:2012-10-14 06:19:30
【问题描述】:

我要读取一个文件夹,统计文件夹中的文件数量(可以是任何类型),显示文件数量,然后将所有文件复制到另一个文件夹(指定)。

我将如何进行?

【问题讨论】:

  • “我是学生”的意思是你应该自己完成作业,而不是交给你的哥哥。至少你应该试一试
  • 我不会再问模糊和不具体的问题,经过一些研究,我会开始编码。如果我遇到困难,我会在这里发帖what I have tried,以及我无法摆脱的错误,然后在帮助下,我会继续我的工作....

标签: java copy directory


【解决方案1】:

我要读取一个文件夹,统计文件夹中的文件个数(可以 是任何类型)显示文件的数量

您可以在 java.io.File 的 javadocs 中找到所有这些功能

然后将所有文件复制到另一个文件夹(指定)

这有点棘手。阅读:Java Tutorial > Reading, Writing and Creating of Files (请注意,此处描述的机制仅在 Java 7 或更高版本中可用。如果 Java 7 不是一个选项,请参阅之前许多类似问题中的一个,例如:Fastest way to write to file?

【讨论】:

    【解决方案2】:

    这里有所有示例代码:

    http://www.exampledepot.com

    http://www.exampledepot.com/egs/java.io/GetFiles.html

    File dir = new File("directoryName");
    
    String[] children = dir.list();
    if (children == null) {
        // Either dir does not exist or is not a directory
    } else {
        for (int i=0; i<children.length; i++) {
            // Get filename of file or directory
            String filename = children[i];
        }
    }
    
    // It is also possible to filter the list of returned files.
    // This example does not return any files that start with `.'.
    FilenameFilter filter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return !name.startsWith(".");
        }
    };
    children = dir.list(filter);
    
    
    // The list of files can also be retrieved as File objects
    File[] files = dir.listFiles();
    
    // This filter only returns directories
    FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory();
        }
    };
    files = dir.listFiles(fileFilter);
    

    抄袭http://www.exampledepot.com/egs/java.io/CopyDir.html

    // Copies all files under srcDir to dstDir.
    // If dstDir does not exist, it will be created.
    public void copyDirectory(File srcDir, File dstDir) throws IOException {
        if (srcDir.isDirectory()) {
            if (!dstDir.exists()) {
                dstDir.mkdir();
            }
    
            String[] children = srcDir.list();
            for (int i=0; i<children.length; i++) {
                copyDirectory(new File(srcDir, children[i]),
                                     new File(dstDir, children[i]));
            }
        } else {
            // This method is implemented in Copying a File
            copyFile(srcDir, dstDir);
        }
    }
    

    然而,这些东西很容易被骗:)

    【讨论】:

    【解决方案3】:

    我知道这为时已晚,但下面的代码对我有用。它基本上遍历目录中的每个文件,如果找到的文件是一个目录,那么它会进行递归调用。 它只计算目录中的文件数

    public static int noOfFilesInDirectory(File directory) {
        int noOfFiles = 0;
        for (File file : directory.listFiles()) {
            if (file.isFile()) {
                noOfFiles++;
            }
            if (file.isDirectory()) {
                noOfFiles += noOfFilesInDirectory(file);
            }
        }
        return noOfFiles;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-10-14
      • 2015-05-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-07
      • 1970-01-01
      • 2013-05-21
      • 2013-11-30
      • 2019-10-09
      相关资源
      最近更新 更多