简体   繁体   English

将文件列表读入流

[英]Read a list of files into stream

I want to validate some links which are generated by my application. 我想验证一些由我的应用程序生成的链接。 These generated files contain loads of lines I want to match with a regex to check if they are correct. 这些生成的文件包含我要与正则表达式匹配的行负载,以检查它们是否正确。

Where I'm stuck: I have a List of my files already declared and I want to open them into a stream so I can do some fancy Java coding with streams. 卡住的地方:我已经声明了一个文件列表,我想将它们打开到流中,以便可以对流进行一些精美的Java编码。

Right now my code looks like this: 现在,我的代码如下所示:

   return !(Files.lines(Path.of(String.valueOf(FileList)))
      .anyMatch(v -> !v.matches(pattern));

The files are in the project's working directory so they only need a filename to access them. 这些文件位于项目的工作目录中,因此只需要文件名即可访问它们。 I want to read the files line by line to check for the incorrect lines. 我想逐行读取文件以检查不正确的行。 Is this the way to read a list of files with streams? 这是读取流文件列表的方法吗? If you know of any other way to make this work with streams please share. 如果您知道通过流进行此工作的任何其他方法,请分享。 Many thanks! 非常感谢!

I have a List of my files already declared 我已经声明了我的文件清单

Assuming that is your FileList variable 1 , and that it is a List<String> (unspecified in question), then you would do something like this: 假设这是您的FileList变量1 ,并且它是List<String> (未指定),那么您将执行以下操作:

Predicate<String> matches = Pattern.compile(pattern).asMatchPredicate();
return FileList.stream().allMatch(f -> {
    try {
        return Files.lines(Paths.get(f)).allMatch(matches);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
});

1) Java naming convention specifies that variable names should start with lowercase letter. 1)Java命名约定指定变量名称应以小写字母开头。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM