Question: Create a method which asks they user for the letter of a drive, and then using that letter, counts the number of files in that
Create a method which asks they user for the letter of a drive, and then using that letter, counts the number of files in that drive. The File object can be used to connect to a directory. For example, new File(f:\\) would create a new File object connected to the F drive (assuming it exists). Once you have this object, three methods which would be useful are: a) listFiles() which returns a File[] array representing all the files and directories in the calling File object; b) isDirectory() which returns true if the file is a directory; and c) isFile() which returns true if the file is a normal file (ie. not a directory). Create a recursive method which will: a) Use the method header: public static int countFiles(String directoryName) b) Use a loop in the recursive method to process every file in the directory. (this is one of those exceptions where a loop is used within the recursive method) . This is done after calling the listFiles() method. c) Within the loop, check each File object in the array returned by listFiles(), and if that file is an actual file (not a directory) count it. If the File object is a directory, recursively count the files in that directory and add the result to the count. d) Return the count. It is also useful to know that the listFiles() method will return null if there are no files in the File object (ie. isFile() is true or it is an empty directory.) The method is supposed to be recursive and in java language.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
