Recursively search for a directory in Java

后端 未结 7 2129
深忆病人
深忆病人 2021-01-05 10:45

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 subdi

相关标签:
7条回答
  • 2021-01-05 10:47

    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.
    0 讨论(0)
  • 2021-01-05 10:54

    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);
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-01-05 10:56

    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.

    0 讨论(0)
  • 2021-01-05 10:58

    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;
        }
    }
    
    0 讨论(0)
  • 2021-01-05 11:01

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

    java.io.File API reference

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题