在Java中匹配两个路径

时间:2018-08-14 13:33:18

标签: java

我正在尝试将路径与任何人都可以帮助的路径流匹配

这是初始代码:

public static void main(String[] args) {
    try (Stream<Path> paths = Files.walk(Paths.get("C:/Users/UCD/eclipse-workspace/metrics_cal/src/test/resources/xerces2-j-Xerces-J_2_8_0"))) 
    {
         Path path1 = Paths.get("\"C:/Users/UCD/eclipse-workspace/metrics_cal/src/test/resources/xerces2-j-Xerces-J_2_8_0/xerces2-j-Xerces-J_2_8_0/src/org/apache/xerces/jaxp/SAXParserImpl.java\";");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }

paths收集了一个文件夹的所有路径,而path1是我要与之匹配的路径,但问题是paths是一个流,而path1不是我该怎么做?

1 个答案:

答案 0 :(得分:1)

您可以使用Stream.anyMatch()

String directory = "C:/Users/UCD/eclipse-workspace/metrics_cal/src/test/resources/xerces2-j-Xerces-J_2_8_0";
String file = "\"C:/Users/UCD/eclipse-workspace/metrics_cal/src/test/resources/xerces2-j-Xerces-J_2_8_0/xerces2-j-Xerces-J_2_8_0/src/org/apache/xerces/jaxp/SAXParserImpl.java\";";
try (Stream<Path> paths = Files.walk(Paths.get(directory))) {
    Path path1 = Paths.get(file);
    if(paths.anyMatch(path1::equals)){
        // do something
    }
} catch (IOException e) {
    e.printStackTrace();
}