import java.io.*; class FileInfo { File path; public FileInfo(String path) { this.path = new File(path); } // ex a public void fileCount() { long fileCount = 0; File[] l = path.listFiles(); for(int i = 0; i < l.length; i++) if(l[i].isFile()) fileCount++; System.out.println(fileCount); } // ex b // It slightly unclear if one should print the sum of the lengths, or the length // of each file. I print the sum as it is slightly more complicated. public void dirSize() { long dirSize = 0; File[] l = path.listFiles(); for(int i = 0; i < l.length; i++) if(l[i].isFile()) dirSize += l[i].length(); System.out.println(dirSize); } // ex c void simpleFilteredFileCount(String acceptedExtension) { long fileCount = 0; File[] l = path.listFiles(); for(int i = 0; i < l.length; i++) if(l[i].isFile() && l[i].getName().endsWith(acceptedExtension)) fileCount++; System.out.println(fileCount); } // ex d void recursiveCount() { long total = recursiveCount( path ); System.out.println( "#files: " + total ); } // ex d private long recursiveCount(File dir) { File[] l = dir.listFiles(); long count = 0; for(int i = 0; i < l.length; i++) if(l[i].isFile()) count++; else if( l[i].isDirectory() ) count += recursiveCount( l[i] ); return count; } }