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

How to get number of files in a directory and subdirectory from a path in 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] 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException:.

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

folderName.txt 有这样的结构。

E:/folder1

E:/folder2

异常因为:

readLine() method reads a entire line from the input but removes the newLine characters from it.so it's unable to split around \n

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

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

D:5

D:\Deployment

D:\HeapDumps

D:\Program Files

D:\Programming

这段代码给你你想要的+你可以根据你的需要修改它

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:5=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);
    }