多个筛选器在路径上,即文件。

huangapple go评论105阅读模式
英文:

Multiple filters on a Path i.e. file

问题

  1. 我对以下代码有一个问题
  2. ```java
  3. import java.io.*
  4. import java.nio.*
  5. import org.springframework.core.io.FileSystemResource;
  6. import org.springframework.core.io.Resource;
  7. public static void FileReader {
  8. String filePath = "C:\\Downloads\\";
  9. Stream<Path> walk = Files.walk(Paths.get(filePath));
  10. List<Resource> result = walk
  11. .map(file -> file.toString())
  12. .filter(file -> file.endsWith(".json"))
  13. .map(file -> new FileSystemResource(file))
  14. .collect(Collectors.toList());
  15. walk.close();
  16. }

你是否可以在文件上有多个过滤器,比如一个用于文件是否以 .zip 结尾?

  1. <details>
  2. <summary>英文:</summary>
  3. I have a question on the following code:
  4. ```java
  5. import java.io.*
  6. import java.nio.*
  7. import org.springframework.core.io.FileSystemResource;
  8. import org.springframework.core.io.Resource;
  9. public static void FileReader {
  10. String filePath = &quot;C:\Downloads\&quot;&quot;;
  11. Stream&lt;Path&gt; walk = Files.walk(Paths.get(filePath));
  12. List&lt;Resource&gt; result = walk
  13. .map(file -&gt; file.toString())
  14. .filter(file -&gt; file.endsWith(&quot;.json&quot;))
  15. .map(file -&gt; new FileSystemResource(file))
  16. .collect(Collectors.toList());
  17. walk.close()
  18. }

Can you have multiple filters on file, such as one for if the file ends in .zip?

答案1

得分: 1

总之 - 是的。

首先,您的筛选器可以包含多个条件,这些条件可以用一些逻辑运算符连接在一起,例如:

  1. List<Resource> result = walk
  2. .map(file -> file.toString())
  3. .filter(file -> file.endsWith(".json") || file.endsWith(".zip"))
  4. .map(file -> new FileSystemResource(file))
  5. .collect(Collectors.toList());

其次,您可以有多个filter调用,并且可以在终止调用之前的任何地方混合它们,例如:

  1. List<Resource> result = walk
  2. .map(file -> file.toString())
  3. .filter(file -> file.endsWith(".json"))
  4. .map(file -> new FileSystemResource(file))
  5. .filter(f -> f != null) // 或者更有意义的条件...
  6. .collect(Collectors.toList());
英文:

In a word - yes.

First, your filter could contain several conditions joined together with some logical operator, e.g.:

  1. List&lt;Resource&gt; result = walk
  2. .map(file -&gt; file.toString())
  3. .filter(file -&gt; file.endsWith(&quot;.json&quot;) || file.endsWith(&quot;.zip&quot;)
  4. .map(file -&gt; new FileSystemResource(file))
  5. .collect(Collectors.toList());

Second, you could have multiple filter calls and you can mix them up anywhere before the terminating call, e.g.:

  1. List&lt;Resource&gt; result = walk
  2. .map(file -&gt; file.toString())
  3. .filter(file -&gt; file.endsWith(&quot;.json&quot;))
  4. .map(file -&gt; new FileSystemResource(file))
  5. .filter(f -&gt; f != null) // Or something more meaningful...
  6. .collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年9月1日 07:10:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63679321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定