java programming.... so you need to recursively iterate through subdirectories o
ID: 3762105 • Letter: J
Question
java programming....
so you need to recursively iterate through subdirectories of a user - specified directory and print out any files in the subdirectories.
Get the root directory from the user and be sure to check for erroneous input:
did the user enter anything? does the path exist? does the path point to a directory?
2) Create a method that will list all items (files and subdirectories) in a given directory. Get an array containing the contents, and for each item,
a) If the item is a file, print its path to the console. If the item is a directory, get an array containing its contents, and for each item,
b) If the item is a file, print its path to the console. If the item is a directory, get an array containing its contents, and for each item, If the item is a file, print its path to the console.
c) If the item is a directory, get an array containing its contents, and for each item, If the item is a file, print its path to the console. If the item is a directory, get an array containing its contents, and for each item, …..
3) Pass the root directory from the user into the method you created and list all files in the root directory and its subdirectories
Explanation / Answer
the below java method can be used for the file listing:
public void filelist(String directory_name, ArrayList<File> files) {
File directory = new File(directory_name);
File[] fList = directory.listFiles();
for (File file : fList) {
// checking if the directory contains a file
if (file.isFile()) {
files.add(file);
}
//checking if directory contains a directory
else if (file.isDirectory()) {
filelist(file.getAbsolutePath(), files);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.