Liz
Liz

Reputation: 635

Recursively search for a directory in Java

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

Upvotes: 8

Views: 24499

Answers (7)

Kong
Kong

Reputation: 9556

In Java 8 via the streams API:

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

The #walk is lazy, so any short-circuiting terminal operation will optimize the IO required.

Upvotes: 10

sushant097
sushant097

Reputation: 3736

Or, you should use the concept of Recursively search the file until it found: 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;
    }
}

Upvotes: 0

Wonil
Wonil

Reputation: 6727

To walk the file tree, FileVisitor interface can be used. Please see the tutorial. Please see Find sample codes also.

Upvotes: 4

cutebug
cutebug

Reputation: 475

As you mentioned recursion should cater to 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);
            }
        }
    }

}

Upvotes: 3

crybird
crybird

Reputation: 111

Divide and conquer? A naive approach: For every directory, you may start a task, it does the following:

  1. list every directory
  2. if the list contains a matching directory, prints and exit the application
  3. start a task for every directory.

Upvotes: 0

Tom
Tom

Reputation: 44821

Something like:

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;
}

Upvotes: 0

Tom
Tom

Reputation: 1221

Your solution will include the use of File.listFiles(String)

java.io.File API reference

Upvotes: 3

Related Questions