0

suppose my directory structure is as follows:-
MainFolder
1.file1
2.Folder1
2.1 file2
2.2 file3
2.3 file4
3 file5
4.Folder2
4.1 file6
4.2 file7

Inside main folder total files are 7 as i know structure so can write code to calculate total no of files in main folder

        public class FileIo {
        public static void main(String[] args) throws IOException {
        int count1=0;
        File f=new File("C:\\Users\\omshanti\\Desktop","mainfolder");
         String[] s=f.list();
         for(String s1:s){
             File f1=new File(f,s1);
             if(f1.isFile()){
                 count1++;
             }
             if(f1.isDirectory()){
                      int subdirfilelength=f1.list().length;
                     count1=count1+subdirfilelength;
                 }

             }
         System.out.println("total no of files in h1 folder "+count1);        


     }
 }

above code works properly and give total no of files as 7
but if i dont know file structure, and folder inside main folder also contain subfolder and that folder contain file, so above code not give correct answers,
for example:-
MainFolder
1.file1
2.Folder1
2.1 subfolder1
2.1.1 SubFolderOfSubFolder
2.1.1.1 file2
2.1.1.2 file3
2.1.1.3 file4 2.2 file5
3 file6
4.Folder2
4.1 file7
4.2 file8
here total no of files is 8,but above code fails

finally i got this solution

            int count1=0;
            File f=new File("C:\\Users\\omshanti\\Desktop","h1");
             String[] s=f.list();
             for(int i=0;i<s.length;i++){
                 File f1=new File(f,s[i]);
                 if(f1.isFile()){
                     count1++;

                 }
                 else if(f1.isDirectory()){
                          String[] subdir1=f1.list();
                          for(int j=0;j<subdir1.length;j++){
                              File f2 = new File(f1, subdir1[j]);
                              if (f2.isFile()) {
                                  count1++;


                              }
                              else if(f2.isDirectory()){
                                  String[] subdir2=f2.list();
                          for(int k=0;k<subdir2.length;k++){
                              File f3 = new File(f2, subdir2[k]);

                              if (f3.isFile()) {
                                  count1++;


                              }
                              else if(f3.isDirectory())
                              {
                          String[] subdir3=f3.list();
                                  for (String subdir31 : subdir3) {
                                      File f4 = new File(f3, subdir31);
                                      if (f4.isFile()) {
                                          count1++;


                                      }
                                        }




                         }
                     }

                              }}}

                 }
             System.out.println("total no of files in h1 folder "+count1);        
  • 1
    [Walking the File Tree](https://docs.oracle.com/javase/tutorial/essential/io/walk.html). You may also want to do some research into method recursion (recursive method calls) – MadProgrammer Jun 05 '16 at 11:06
  • 1
    For [example](http://stackoverflow.com/questions/12505491/list-files-from-directories-and-sub-directories-in-java-including-only-partial-f/12505570#12505570), [example](http://stackoverflow.com/questions/28580764/listfiles-in-java-fails-to-get-all-files/28581269#28581269) – MadProgrammer Jun 05 '16 at 11:11

1 Answers1

0

You can use apache commons-io's FileUtils.listFiles for this:

List<File> files = (List<File>) FileUtils.listFiles(new File("/your/path"), TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
int nbFiles = files.size();

Their javadoc says:

Finds files within a given directory (and optionally its subdirectories).

The first parameter is your path. The 2nd sets the flag to 'find files'. The 3rd sets the flag to 'consider directories', thus going through subdirectories.

You need the following dependency in maven:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

or download the jar here and add it to your classpath: commons-io:commons-io:2.4 (or direct link to download)

FileUtils javadoc on Apache's site

alexbt
  • 16,415
  • 6
  • 78
  • 87
  • i solved my problem by myself in simple way – Hemant Dhanuka Jun 05 '16 at 12:36
  • could you post what you did (perhaps add an "edit" section to your question?) – alexbt Jun 05 '16 at 12:38
  • good. I personally think the 2 liners with commons-io is way more elegant, but hey, if it suits your need. For info, instead of FileUtils.listFiles, if the content is too large, you could also use FileUtils.iterateFilesAndDirs and iterate manually.. – alexbt Jun 05 '16 at 12:58