简体   繁体   English

如何使用java在给定路径的超级目录中查找特定文件夹

[英]How to find a specific folder in its super directory of a given path using java

  • How to find a specific folder in its super directory of a given path using java?如何使用java在给定路径的超级目录中查找特定文件夹?
  • For Example I need to find a folder which holds name as : "Incoming" in the given path as : D:\\test\\28-4-2016\\spr\\5526283_1\\ItemFile\\1446026507776_1\\ftp\\content-providers\\spr-e\\data\\incoming\\EHPP-17-2-2015\\EHPP-17-2-2015例如,我需要在给定的路径中找到一个名称为“传入”的文件夹:D:\\test\\28-4-2016\\spr\\5526283_1\\ItemFile\\1446026507776_1\\ftp\\content-providers\\spr-e \\data\\incoming\\EHPP-17-2-2015\\EHPP-17-2-2015
  • I need to find the folder and its path.我需要找到文件夹及其路径。
  • My present code not finding the folder.我目前的代码没有找到文件夹。
  • Can anyone help me to Achieve This?任何人都可以帮助我实现这一目标吗?

-------------------Code snippet starts-------------------------- public static void find() { -------------------代码片段开始-----------公共静态无效查找(){

        File dir = new File("D:\\test\\28-4-2016\\apa\\5346560_1 
  \\ItemFile\\1444799103007_1\\ftp\\content-providers\\apa-e\\data
  \\incoming\\CBS_v47i4");
    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("incoming")) {
            foundFolder = true;
            System.out.println("Incoming Folder found : ");
            System.out.println("Incoming filder path : 
  "+file.getAbsolutePath());
            System.out.println("Parent path :  " +file.getParent());
            break;
        }
        if(file.isDirectory()) {
           findDirectory(file);
        }
    }
    }
    }

Change your findDirectory method as follows更改您的findDirectory方法如下

public static void find() {
    File dir = new File(
            "D:\\test\\28-4-2016\\apa\\5346560_1\\ItemFile\\1444799103007_1\\ftp\\content-providers\\apa-e\\data\\incoming\\CBS_v47i4");
    foundFolder = findDirectory(dir);
}

private static boolean findDirectory(File parentDirectory) {
    System.out.println(parentDirectory.getPath());
    File[] files = parentDirectory.listFiles();
    for (File file : files) {
        if (!file.isFile()) {

            if (file.getName().equals("incoming")) {

                System.out.println("Incoming Folder found : ");
                System.out.println("Incoming filder path : " + file.getAbsolutePath());
                System.out.println("Parent path :  " + file.getParent());
                return true;
            } else if (file.isDirectory()) {
                return findDir(file);
            }
        }
    }
    System.out.println("Incoming Folder not found : ");
    return false;
}

There seems to be a typo in your example and the hard-coded path?您的示例和硬编码路径中似乎有错字? Also, use Path.pathSeperator() rather than double splash.此外,使用 Path.pathSeperator() 而不是双飞溅。 I would suggest you recursively, navigate to sub-folders and output all files and directories to debug.我建议您递归地导航到子文件夹并输出所有文件和目录以进行调试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用java查找webapps目录的路径 - How to find the path of webapps directory using java 如何使用Java在给定路径下创建新文件夹? - How to create a new folder under given path using Java? 程序将文件夹的特定路径打印到Java中的下一个子文件夹中 - Program To print specific path of folder into its next sub folder in java 如何在Java目录及其子目录中查找/列出具有特定名称的文件或目录? - How do I find/list a file or directory with specific name inside a directory and its subdirectories in Java? Java - 在给定文件夹中查找特定文件(基于条件) - Java - Find specific files (based on conditions) inside a given folder 如何从java中的文件夹路径中获取文件夹目录 - How to get a folder directory from folder path in java 如何通过在 Java 中给出其路径的子集来获取目录的完整路径? - How to get complete path of a directory by giving a subset of its path in Java? 如何根据 java 中传递的关键字并在 memory 方法中加载找到特定目录及其文件 - How to find specific directory and its files according to the keyword passed in java and loading in memory approach 如何使用Java查找应用程序安装路径目录 - How to find an application installation path directory with java 如何在不使用包的情况下识别给定路径是Java中的文件还是目录? - How to identify whether a given path is either file or directory in Java without using package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM