Question: Write a public static method fileCount that accepts a String representing a path on your computer as a parameter and returns the number of files


Write a public static method fileCount that accepts a String representing a path on your computer as a parameter and returns the number of files in that path, including files in sub-folders, and sub-folders of sub-folders, etc... to all depths within. Example use shown below: String root - "C:\Users\Home\Documents BCTSummer 2018"; //String root "C:\\"; // this takes WAY too long to finish System.out.println("Number of files" + Quiz12.fileCount(root)); // 1789 files counted Since we now have experience with File objects, arrays, and the for-each loop, the following starter code could be of tremendous help here: public static void main(String[I args) throws FileNotFoundException i //String root "C:\\"; int fileCount0 int folderCount -0 // C: drive is way too huge, program runs forever, so try sub-folder: String root = "C:\\Users\\Home\\Documents\XBC\\Summer 2018"; File start = new File(root); FileL] arrayOfFiles -start.listFilesO; for (File found: arrayOfFiles) if (found.isFileO) fileCount++ else folderCount++; System.out.printlnC"Number of files fileCount); System.out.println("Number of foldersfolderCount); 3// this counts 5 files and 18 folders, which is not correct // it never counts files in sub-folders of sub-folders of sub-folder
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
