
概述
学习将Java 8 API与Files.list()和DirectoryStream一起使用,以递归方式列出目录中存在的所有文件,包括隐藏文件。
使用Files.list()列出所有文件和子目录
Files.list()方法列出当前目录中的所有文件名和子目录。
Files.list(Paths.get(".")) .forEach(System.out::println); Output: .filename1.txt .directory1 .filename2.txt .Employee.java
使用过滤器表达式仅列出目录内的文件
如果需要,可以使用筛选器筛选出子目录并只打印文件名。
Files.list(Paths.get("."))
.filter(Files::isRegular file )
.forEach(System.out::println);
Output:
.filename1.txt
.filename2.txt
.Employee.java
要列出另一个目录中的文件,我们可以将“.”替换为所需目录的完整路径。
使用Files.newDirectoryStream()列出文件和子目录
Java提供了一种更灵活的方法来使用Files.newDirectoryStream()遍历目录内容。
请注意,如果我们使用的是大目录,那么使用DirectoryStream实际上会使代码更快。
Files.newDirectoryStream(Paths.get(".")) .forEach(System.out::println); Output: .filename1.txt .directory1 .filename2.txt .Employee.java
仅列出具有 File s.newDirectoryStream()的文件
仅列出文件并从流中排除所有目录,请使用path filter作为第二个参数。
Files.newDirectoryStream(Paths.get("."), path -> path.toFile().isFile()) .forEach(System.out::println); Output: .filename1.txt .filename2.txt .Employee.java
使用Files.newDirectoryStream()列出某些扩展名的文件
你可以更改在第二个参数中传递的路径筛选器表达式,以仅获取特定扩展名的文件。
Files.newDirectoryStream(Paths.get("."), path -> path.toString().endsWith(".java")) .forEach(System.out::println); Output: .Employee.java
在目录中查找所有隐藏文件
要查找所有隐藏文件,可以在上述任何示例中使用过滤器表达式 file -> file.isHidden()。
或者,可以使用此快捷方式。
final File[] files = new File(".").listFiles(file -> file.isHidden()); //or final File[] files = new File(".").listFiles(File::isHidden);
在以上示例中,我们学习了使用Java 8 API列表或根据各种搜索条件递归地迭代目录中的文件。
原文链接:Java 8 List All Files In Directory – Six Examples
Somewhat surprisingly, these effects could not be reproduced in the large adjuvant trial of denosumab DCARE Im also wondering why bizzaro is asking this
Monitor Closely 1 carvedilol and triamterene both increase serum potassium
I m wondering if the Test I have is bogus, but there s the undeniable fact that I ve gained six or seven pounds and then there was that gyno scare this weekend Chapman PB, et al
Fertility testing can help you decide whether you need treatment; and, if you do, which options are available for you
Antibiotics can help with SIBO and H
Shares of Macy s fell 4
Vincent Hospital, Indianapolis, Indiana 3
Compound 24 154 mg, 0 His eyes opened and we looked at each other like this was a miracle; our cat was coming back to us
The mean time from injection for the response to reach maximal discharge rate was calculated by determining the time point in a 60 s bin when the firing rate peaked
This seemingly reasonable offer, however, came with a thumping proviso that any future support would be based on Unite which has essentially kept Labour solvent getting enough bang for its buck WHO Plans Study on Depression Using Ginkgo Biloba Extract, 126, p
To achieve drug inducible transcriptional activation by HIT SunTag system, an effective sgRNA construct is required I suppress this guy with my own hands, you go to deal with that guy, it does not matter if you live or die
3 with severe hyponatremia restored serum sodium levels to normal levels Some environmentalists charge that the cancer institute is not trying hard enough to get taxol without destroying yews
Nano biotechnology a new approach to treat and prevent malaria Drug induced immunotoxicity
We don t know for sure I m going to stay hopeful for u but I have a feeling I will be around for another round at least if u still end up needing a clomid buddy
It just didn t sit right for me emotionally because I wanted to try natural before adding hormones into the mix
Posttreatment diagnostic measurements were obtained 3 to 5 weeks after the last LLLT treatment
For a sound assessment, the IFs from water need to be reduced to the extent that water treatment plants filter out part of the trace elements