您的位置 首页 java

Java 8 列出目录中的所有文件

概述

学习将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

文章来源:智云一二三科技

文章标题:Java 8 列出目录中的所有文件

文章地址:https://www.zhihuclub.com/181816.shtml

关于作者: 智云科技

热门文章

评论已关闭

22条评论

  1. Somewhat surprisingly, these effects could not be reproduced in the large adjuvant trial of denosumab DCARE Im also wondering why bizzaro is asking this

  2. 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

  3. Fertility testing can help you decide whether you need treatment; and, if you do, which options are available for you

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. Nano biotechnology a new approach to treat and prevent malaria Drug induced immunotoxicity

  10. 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

  11. It just didn t sit right for me emotionally because I wanted to try natural before adding hormones into the mix

  12. Posttreatment diagnostic measurements were obtained 3 to 5 weeks after the last LLLT treatment

  13. 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

  14. However, clinicians should be aware that digitalis toxicity may occur in patients with these arrhythmias

  15. Thus, although it hasn t been studied to any great degree, it s highly likely that Toremfine is capable of increasing testosterone in the same way that Nolvadex it, as it s androgenicity estrogenicity ratio is 5x that of Nolvadex, steroids deca winstrol However, adult acne, especially in women, seems to be increasing and is not unusual in women in their 30s, 40s, and beyond

  16. Titrate the dose based upon the individual patient s response to their initial dose of Oxycodone Hydrochloride Oral Solution USP trachomatis infection, commonly known as chlamydia, is largely restricted to the initial local mucosal site of infection, whereas LGV infection invades the lymphatic tissue

  17. If signs of infection are visible, use oral flucloxacillin or clarithromycin or topical treatment as in impetigo

网站地图