【问题标题】:How to get number of files in a directory and subdirectory from a path in java?如何从java中的路径获取目录和子目录中的文件数?
【发布时间】:2015-07-11 14:25:26
【问题描述】:

我有一个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:/文件夹1

E:/folder2

【问题讨论】:

  • in.readLine(); 读取一行,所以它不能有行分隔符\n,这意味着split("\n") 是多余的。
  • Best way to read a text file 的可能重复项

标签: java arrays file readline


【解决方案1】:

例外因为:

readLine() 方法从输入中读取整行但删除 来自它的 newLine 字符。因此它无法在 \n

周围分割

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

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

D:\305

D:\部署

D:\HeapDumps

D:\程序文件

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);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-20
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 2013-02-02
    • 1970-01-01
    相关资源
    最近更新 更多