Recursively look up a directory in Java

What is the best way to find a directory with a specific name in Java? The directory I'm looking for can be located either in the current directory or in one of its subdirectories.

+6
source share
7 answers

Your solution will include using File.listFiles(String)

java.io.File API Link

+2
source

In Java 8, via the streams API :

 Optional<Path> hit = Files.walk(myPath) .filter(file -> file.getFileName().equals(myName)) .findAny(); 

#walk is lazy, so any short circuit operation will optimize the required input / output value.

+5
source

You can use FileVisitor to navigate the file tree. See tutorial . See Find Code Examples .

+4
source

As you mentioned, recursion must satisfy this requirement

 import java.io.File; public class CheckFile { private static boolean foundFolder = false; public static void main(String[] args) { File dir = new File("currentdirectory"); findDirectory(dir); } private static void findDirectory(File parentDirectory) { if(foundFolder) { return; } File[] files = parentDirectory.listFiles(); for (File file : files) { if (file.isFile()) { continue; } if (file.getName().equals("folderNameToFind")) { foundFolder = true; break; } if(file.isDirectory()) { findDirectory(file); } } } } 
+2
source

Divide and win? Naive approach: for each directory you can run a task, it does the following:

  • list of all directories
  • if the list contains the corresponding directory, prints and exits the application
  • run a task for each directory.
0
source

Sort of:

 public static final File findIt(File rootDir, String fileName) { File[] files = rootDir.listFiles(); List<File> directories = new ArrayList<File>(files.length); for (File file : files) { if (file.getName().equals(fileName)) { return file; } else if (file.isDirectory()) { directories.add(file); } } for (File directory : directories) { File file = findIt(directory); if (file != null) { return file; } } return null; } 
0
source

Or you should use the concept of a recursive file search until you find: Here is the code:

 String name; //to hold the search file name public String listFolder(File dir) { int flag; File[] subDirs = dir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); System.out.println("File of Directory: " + dir.getAbsolutePath()); flag = Listfile(dir); if (flag == 0) { System.out.println("File Found in THe Directory: " + dir.getAbsolutePath()); Speak("File Found in THe Directory: !!" + dir.getAbsolutePath()); return dir.getAbsolutePath(); } for (File folder : subDirs) { listFolder(folder); } return null; } private int Listfile(File dir) { boolean ch = false; File[] files = dir.listFiles(); for (File file : files) { Listfile(file); if (file.getName().indexOf(name.toLowerCase()) != -1) {//check all in lower case System.out.println(name + "Found Sucessfully!!"); ch = true; } } if (ch) { return 1; } else { return 0; } } 
0
source

Source: https://habr.com/ru/post/916820/


All Articles