0

I Have a folderName.txt file which has a list of all the folders path. How can i get the total count of the total number of files present inside that folder path.

For one path i'm able to do this like this.

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

But the Problems is i'm not able to read the path from the folderName.txt file .

i'm trying this

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

           while(line != null){

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

But when i'm trying to access the linesFile array i'm getting an exception. like linesFile[1] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:.

My question is why i'm getting the java.lang.ArrayIndexOutOfBoundsException ?. and How could i read individual directory path and total number of files inside it. Is there way to read the total number of files in a subdirectory too.

folderName.txt has a structure like this.

E:/folder1

E:/folder2

Community
  • 1
  • 1
  • `in.readLine();` reads one line, so it can't have line separator `\n` in it, which means `split("\n")` is redundant. – Pshemo Jul 11 '15 at 14:28
  • possible duplicate of [Best way to read a text file](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file) – Joe Jul 11 '15 at 14:30

1 Answers1

2

Exception because :

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

Here is the code That does exactly what you want,.

I have a folderPath.txt which has a list of Directory like this.

D:\305

D:\Deployment

D:\HeapDumps

D:\Program Files

D:\Programming

This code gives you what you want + you can Modify it according to your need

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

}

Output:

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

Edit : This counts the total number of files present inside the subdirectory too.

/**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);
    }
Ankur Anand
  • 3,699
  • 1
  • 21
  • 39