enter code hereI wrote the following code which searches a folder directory recursively to find a specific folder. The program is supposed to do check the folder name and if the folder name is "src", then it should go into that folder to get all the files. Currently the program is getting all the files from all the directories.
public class Main { public static void main(String[] args) { File fileObject = new File("C:\\Users\\lizzie\\Documents\\"); recursiveTraversal(fileObject); } public static void recursiveTraversal(File fileObject) { if (fileObject.isDirectory()) { File allFiles[] = fileObject.listFiles(); for(File aFile : allFiles){ recursiveTraversal(aFile); } } else if (fileObject.isFile()) { System.out.println(fileObject.getAbsolutePath()); } } }
when I check if a certain folder is a directory, I added the following constraint but that didn't help.
if (fileObject.isDirectory() && fileObject.getName().equals("src"))` Please let me know what I can do to improve my code. Anything will be appreciated. Thanks