如何从Java中的路径获取目录和子目录中的文件数?

用户名

我有一个folderName.txt包含所有文件夹路径列表的文件。我如何获取该文件夹路径中存在的文件总数的总数。

对于一个路径,我能够做到这一点。

new File("folder").listFiles().length.

但是问题是我无法从folderName.txt文件读取路径

我正在尝试这个

File objFile = objPath.toFile();
     try(BufferedReader in = new BufferedReader(
                             new FileReader(objFile))){
          String line = in.readLine();

           while(line != null){

            String[] linesFile = line.split("\n");

但是,当我尝试访问linesFile数组时,出现了异常。就像linesFile[1]线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:一样。

我的问题是为什么我要获取java.lang.ArrayIndexOutOfBoundsException?。以及如何读取单个目录路径和其中的文件总数。也有办法读取子目录中的文件总数。

folderName.txt具有这样的结构。

E:/文件夹1

E:/ folder2

安库尔·阿南德(Ankur anand)

异常是因为:

readLine()方法从输入中读取整行,但从其中删除newLine字符,因此无法在\ n周围拆分

这是完全符合您想要的代码。

我有一个folderPath.txt这样的目录列表。

D:\ 305

D:\部署

D:\ HeapDumps

D:\ Program档案

D:\编程

这段代码为您提供了您想要的+您可以根据需要对其进行修改

public class Main {

public static void main(String args[]) throws IOException {

    List<String> foldersPath = new ArrayList<String>();
    File folderPathFile = new File("C:\\Users\\ankur\\Desktop\\folderPath.txt");

    /**
     * Read the folderPath.txt and get all the path and store it into
     * foldersPath List
     */
    BufferedReader reader = new BufferedReader(new FileReader(folderPathFile));
    String line = reader.readLine();
    while(line != null){
        foldersPath.add(line);
        line = reader.readLine();
    }
    reader.close();

    /**
     * Map the path(i.e Folder) to the total no of 
     * files present in that path (i.e Folder)
     */
    Map<String, Integer> noOfFilesInFolder = new HashMap<String, Integer>();
    for (String pathOfFolder:foldersPath){
        File[] files2 = new File(pathOfFolder).listFiles();//get the arrays of files
        int totalfilesCount = files2.length;//get total no of files present
        noOfFilesInFolder.put(pathOfFolder,totalfilesCount);
    }

    System.out.println(noOfFilesInFolder);
}

}

输出:

{D:\Program Files=1, D:\HeapDumps=16, D:\Deployment=48, D:\305=4, D:\Programming=13}

编辑:这也计算子目录中存在的文件总数。

/**This counts the
         * total number of files present inside the subdirectory too.
         */
        Map<String, Integer> noOfFilesInFolder = new HashMap<String, Integer>();
        for (String pathOfFolder:foldersPath){
            int filesCount = 0;
            File[] files2 = new File(pathOfFolder).listFiles();//get the arrays of files
            for (File f2 : files2){
                if (f2.isDirectory()){
                    filesCount += new File(f2.toString()).listFiles().length;

                }
                else{
                    filesCount++;
                }
            }
            System.out.println(filesCount);
            noOfFilesInFolder.put(pathOfFolder, filesCount);
        }

        System.out.println(noOfFilesInFolder);
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

获取目录及其子目录中的文件数

计算目录和子目录中的文件数

如何获取子目录中包含特定目录字符串的子目录中python文件的路径

如何使用Java的目录流仅在目录中而不是其他子目录中获取文件/子目录

如何计算子目录中的文件数?

如何报告所有子目录中的文件数?

如何在Java中获取文件路径的中间子目录?

从目录和子目录中仅获取重复文件

如何从文件中获取子目录列表,然后在目录中创建这些子目录?

使用路径获取Java中的特定子目录

在目录和所有子目录中获取Java文件

查找子目录中的最大文件数

使用纯c ++(跨平台)获取特定路径中的所有目录/文件和子目录

如何获取文件的父目录和子目录

如何删除子目录中的文件

如何遍历子目录中的文件?

如何从Java的子目录中读取文件?

如何从python中的路径拆分和分离根目录和子目录

子目录中的文件?

如何在批处理文件中引用子目录,该子目录的路径取决于用户帐户?

如何遍历目录中的子目录并计算python中子目录中的文件

如何计算所有子目录中的文件数,并将其相加

如何遍历子目录和子目录中的文件,然后在 bash 中打印文件名

对目录和子目录中的每个文件执行操作

同时处理目录和子目录中的文件

通过目录和子目录中的文件的脚本

Linux shell 选择目录和子目录中的文件

每个目录和子目录中的可疑 .htaccess 文件

在目录和子目录中搜索丢失的文件