0

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

2
  • By "get" do you mean "print the name of"? Commented May 5, 2011 at 1:35
  • Yes I mean it will print the file name. Commented May 5, 2011 at 1:40

3 Answers 3

2

If you look at your if-else inside recursiveTraversal, you'll see that you're printing anything that isn't a directory, regardless of where it is. Here's a fix:

public class Main { public static void main(String[] args) { File fileObject = new File("C:\\Users\\lizzie\\Documents\\"); recursiveSearch(fileObject); } public static void recursiveSearch(File fileObject) { if (fileObject.isDirectory()) { if (fileObject.getName().equals("src")) { recursivePrint(fileObject); } else { File allFiles[] = fileObject.listFiles(); for(File aFile : allFiles){ recursiveSearch(aFile); } } } // ignore non-directory objects during search } public static void recursivePrint(File fileObject) { if (fileObject.isDirectory()) { File allFiles[] = fileObject.listFiles(); for(File aFile : allFiles){ recursivePrint(aFile); } } else if (fileObject.isFile()) { System.out.println(fileObject.getAbsolutePath()); } } } 

This will print all the files recursively of any directory named src.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but that didn't work. your code entered an infinite loop. :(
1

What you need to do is put the constraint on what's being printed, not what's being traversed. As you've noticed, the traversal is working fine, since it gets all files in all subfolders.

If you want to print only the filenames inside of the "src" directory (not in subdirectories), then you can do...

... else if (fileObject.isFile() && fileObject.getParent().getName().equals("src") { System.out.println(fileObject.getAbsolutePath()); } ... 

If you want to print what's in the "src" directory, and all subdirectories, then you'll need to break your algorithm into two parts

  1. find the "src" folder, then
  2. use your current algorithm to print everything in all directories from there and lower

Comments

0

Instead of checking for .equals() on the name, check if the name contains "src" using either fileObject.getName().contains(StringBuffer) or fileObject.getName().indexOf("src") != -1

1 Comment

Tried that but nothing happened. Should I check the directory name when I checking if a certain file is a directory?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.