Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基于模式的递归查找目录_Java_File_File Io_Nio - Fatal编程技术网

Java 基于模式的递归查找目录

Java 基于模式的递归查找目录,java,file,file-io,nio,Java,File,File Io,Nio,给定类似于上面的内容,或者使用Apache IO或Java NIO,我如何递归地查找与以下模式匹配的目录: 2017年10月3日(基本上类似于最后一个工作日) 我的结构类似于/sourcefolder/clientfolders/COB03Oct2017/file.pdf 有许多clientfolders和许多cobddmyyyy文件夹 假设我已经有了一个方法,它给了我cob文件夹的名称 如何找到所有客户端文件夹的所有匹配cob文件夹 public static printFnames(Stri

给定类似于上面的内容,或者使用Apache IO或Java NIO,我如何递归地查找与以下模式匹配的目录:

2017年10月3日(基本上类似于最后一个工作日)

我的结构类似于
/sourcefolder/clientfolders/COB03Oct2017/file.pdf

有许多clientfolders和许多cobddmyyyy文件夹

假设我已经有了一个方法,它给了我cob文件夹的名称

如何找到所有客户端文件夹的所有匹配cob文件夹

public static printFnames(String sDir) {
    Files.find(Paths.get(sDir), 999, (p, bfa) -> bfa.isRegularFile()).forEach(System.out::println);
}

这就是我试图在SourceFolder中找到的特定文件夹。我用过Java的

这将返回包含字符串“YourString”作为名称的所有文件夹。在这种情况下,如果要将名称与regex匹配,则需要将方法
.equalsIgnoreCase(“YourString”)
更改为
.matches(“YourRegex”)
。我认为这应该行得通


干杯。

另一种方法可以是递归方法,它将根据需要挖掘到指定的文件夹:

public abstract class ChooseFile {

public static File parent = new File("your/path/name");

public static void main(String args[]) throws IOException {

    printFnames(parent.getAbsolutePath());
}

public static void printFnames(String sDir) throws IOException {

    // Take action only when parent is a directory
    if (parent.isDirectory()) {
        File[] children = parent.listFiles(new FileFilter() {
            public boolean accept(File file) {

                if (file.isDirectory() && file.getName().equalsIgnoreCase("YourString")) // I have serached for "bin" folders in my Source folder.
                    System.out.println(file.getAbsolutePath());
                else if (file.isDirectory())
                    try {
                        parent = file;
                        printFnames(file.getAbsolutePath());
                    }
                    catch (IOException exc) {
                        // TODO Auto-generated catch block
                        exc.printStackTrace();
                    }
                return file.isDirectory() || file.getName().toLowerCase().contains("YourString");
            }
        });

      }
  }

}
publicstaticvoidmain(字符串[]args){
//要搜索的目录。它将搜索整个树
//因此,它也适用于sourcePath=“c:\\”;
字符串sourcePath=“c:\\sourcePath\\”;
字符串cobPattern=“COB03Oct2017”;
List cobDirectories=getCobDirectories(sourcePath,cobbattern);
cobDirectories.forEach(p->System.out.println(p));//检查输出
}
私有静态列表GetCobDirectory(字符串源路径、字符串cobPattern){
List cobDirs=new ArrayList();
GetCobDirectory(源路径、cobPattern、CobDir);
返回cobDirs;
}
私有静态void getCobDirectories(String sourcePath、String cobPattern、List Cobdir){
文件文件=新文件(源路径);
如果(!file.isDirectory()){//仅在文件夹中搜索
回来
}
如果(file.getName().equals(cobPattern)){//添加到集合
add(path.get(sourcePath));
回来
}
if(file.list()==null){//用于抽象路径或错误
回来
}
用于(字符串文件名:file.list()){
getCobDirectory((sourcePath+“\\”+文件名)、cobPattern、cobDirs);
}
}

因此,在本例中,您希望所有
COB003OCT2017
文件夹位于父文件夹
sourcefolder
下,以及不同的
clientfolders
?@prolalistator是-首先获取源文件夹下的所有客户端文件夹,然后查找COB文件夹。其目的是只在最新的COB文件夹中查找文件,因为我正在处理一个非常大的目录树。提供的代码是否按预期工作?它完成了我希望它完成的任务。但是,当一个clientfolder不可访问时,会抛出
java.nio.file.AccessDeniedException
。是否可以重新考虑上述内容或进一步提高效率?是否要扫描所有客户端目录?或者只是其中的一部分?
    @Test
    public void testFiles() {

        String sourcePath = "\\\\server\\pathToCustomerReports\\";

        String cobPattern = "COB" + DateHelper.getPreviousWorkingDay();

        List<Path> clientDirectories = null;

        try {
            clientDirectories = Files.find(Paths.get(sourcePath), 1,
                    (path, bfa) -> bfa.isDirectory())
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }


        List<Path> cobDirectories = new ArrayList<>();

        clientDirectories.forEach(path -> cobDirectories.addAll(getCobdirs(path)));

        System.out.println("Done");


    }

    private List<Path> getCobdirs(Path path) {

        List<Path> cobDirs = new ArrayList<>();

        String cobPattern = "COB" + DateHelper.getPreviousWorkingDay();

        try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
            for (Path p : stream) {
                if (path.toFile().isDirectory() && p.getFileName().toString().equals(cobPattern)) {
                    cobDirs.add(p);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cobDirs;
    }
public abstract class ChooseFile {

public static File parent = new File("your/path/name");

public static void main(String args[]) throws IOException {

    printFnames(parent.getAbsolutePath());
}

public static void printFnames(String sDir) throws IOException {

    // Take action only when parent is a directory
    if (parent.isDirectory()) {
        File[] children = parent.listFiles(new FileFilter() {
            public boolean accept(File file) {

                if (file.isDirectory() && file.getName().equalsIgnoreCase("YourString")) // I have serached for "bin" folders in my Source folder.
                    System.out.println(file.getAbsolutePath());
                else if (file.isDirectory())
                    try {
                        parent = file;
                        printFnames(file.getAbsolutePath());
                    }
                    catch (IOException exc) {
                        // TODO Auto-generated catch block
                        exc.printStackTrace();
                    }
                return file.isDirectory() || file.getName().toLowerCase().contains("YourString");
            }
        });

      }
  }

}
public static void main(String[] args) {

    //the directory to search. It will search the whole tree
    //so it should work also for sourcePath = "c:\\";
    String sourcePath = "c:\\sourcepath\\";
    String cobPattern = "COB03Oct2017";

    List<Path> cobDirectories = getCobDirectories(sourcePath, cobPattern);
    cobDirectories.forEach(p ->  System.out.println(p)); //check output
}

private static List<Path> getCobDirectories(String sourcePath, String cobPattern) {
    List<Path> cobDirs =  new ArrayList<>();
    getCobDirectories(sourcePath,cobPattern, cobDirs);
    return cobDirs;
}

private static void getCobDirectories(String sourcePath, String cobPattern, List<Path> cobDirs) {

    File file = new File(sourcePath);

    if( ! file.isDirectory()) {//search only in folders 
        return;
    }

    if(file.getName().equals(cobPattern)) {//add to collection 
        cobDirs.add(Paths.get(sourcePath));
        return;
    }

    if(file.list() == null) {//for abstract path or errors 
        return;
    }

    for (String fileName: file.list() ){

        getCobDirectories((sourcePath+"\\"+fileName),cobPattern, cobDirs);
    }
}