当前位置:网站首页>26file filter anonymous inner class and lambda optimization
26file filter anonymous inner class and lambda optimization
2022-07-06 05:27:00 【dengfengling999】
practice :
Traverse and print multi-level directories , Traverse C:abc Folder , namely abc The subfolders of the folder , as long as .java Final document
C:\abc
C:\abc\a
C:\abc\a\a.java
C:\abc\a\a.txt
C:\abc\abc.java
C:\abc\abc.txt
C:\abc\b
C:\abc\b\b.java
C:\abc\b\b.txt
package demo26file Filter anonymous inner class ;
/*
practice :
Traverse and print multi-level directories , Traverse C:abc Folder , namely abc The subfolders of the folder , as long as .java Final document
C:\abc
C:\abc\a
C:\abc\a\a.java
C:\abc\a\a.txt
C:\abc\abc.java
C:\abc\abc.txt
C:\abc\b
C:\abc\b\b.java
C:\abc\b\b.txt
We can use filters to optimize :
stay File There are two and... In the class listFiles Overloaded method , What method parameters pass is a filter
File[] listFiles(FileFilter filter) java.io.FileFilter Interface : Used for abstract pathnames (File object ) Filter
effect : For filtering files (File object )
Abstract method : Method for passing files boolean accept(File pathname) Test whether the specified Abstract path name should be included in a path list
Parameters :File pathname: Use ListFiles Method to traverse the directory , Every file object obtained
File[] listFiles(FilenameFilter filter) java.io.FilenameFilter Interface ; Class instances that implement this interface can be used to filter file names
effect : Used to filter abstract names
Abstract method : Methods for filtering Abstract files boolean accept(File dir,String name) Test whether the specified folder should be included in a file list
Parameters :File dir: The traversed directory passed in the constructor
String name: Use listFiles Method to traverse the directory , Get the name of each file or folder
Be careful : The two filter interfaces do not implement classes , We need to write implementation classes , Rewrite the filtering rules
*/
import demo25file filter .FileFiletermpl;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
public class DemoRecurison {
public static void main(String[] args) {
File file=new File("C:\\abc");
getAllFile(file);
}
private static void getAllFile(File dir){
//System.out.println(dir);// Print the name of the traversed Directory
/* File[] files=dir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
// Filtering rules ,pathname It's a document , Or is it .java The end of the file returns true
return pathname.isDirectory() || pathname.getName().toLowerCase().endsWith(".java");
}
});// Pass filter object
// Use Lamdba Expression optimization anonymous inner class
File[] file=dir.listFiles((File pathname)->{
// Filtering rules ,pathname It's a document , Or is it .java The end of the file returns true
return pathname.isDirectory() || pathname.getName().toLowerCase().endsWith(".java");
});*/
/*
listFiles The method is done in total 3 thing :
1.listFiles Method will traverse the directory passed in the constructor , Get every file in the directory / Folder -- Encapsulated in the File object
2.listFiles Method will call accept Method
3.listFiles Method will traverse every File object , Pass to accept Method parameters pathname
accept Method returns a Boolean value ,true Will pass the past object ( Folder 、.java Final document ) Save to Files Array false You won't pass on the past File Object is saved in the array
*/
/* File[] files=dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// Filtering rules ,pathname It's a document , Or is it .java The end of the file returns true
return new File(dir,name).isDirectory() || name.toLowerCase().endsWith(".java");
// File(dir,name).isDirectory() Combine file objects according to directories
}
});*/
// Use Lamdba Expression optimization anonymous inner class ( There is only one abstract method in the interface )
File[] file=dir.listFiles((File d, String name)->{
return new File(d,name).isDirectory() || name.toLowerCase().endsWith(".java");
// File(dir,name).isDirectory() Combine file objects according to directories
});
for (File f:file){
if (f.isDirectory()){//f It's a folder , Then traverse the entire folder
getAllFile(f);// Recursively call itself
}else{
System.out.println(f);
}
}
}
}
result :

边栏推荐
- [detailed explanation of Huawei machine test] statistics of shooting competition results
- Three.js学习-光照和阴影(了解向)
- MySQL time processing
- Three. JS learning - light and shadow (understanding)
- 【华为机试真题详解】检查是否存在满足条件的数字组合
- Easy to understand I2C protocol
- Jvxetable implant j-popup with slot
- 注释、接续、转义等符号
- 【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
- MySQL if and ifnull use
猜你喜欢

Codeforces Round #804 (Div. 2) Editorial(A-B)

flutter 实现一个有加载动画的按钮(loadingButton)

Vulhub vulnerability recurrence 69_ Tiki Wiki

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Idea one key guide package

01. Project introduction of blog development project

Easy to understand I2C protocol

Leetcode dynamic planning day 16

注释、接续、转义等符号

Simple understanding of interpreters and compilers
随机推荐
注释、接续、转义等符号
LeetCode_字符串反转_简单_557. 反转字符串中的单词 III
03. 开发博客项目之登录
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
Check the useful photo lossless magnification software on Apple computer
Detailed summary of SQL injection
2022 half year summary
Vulhub vulnerability recurrence 67_ Supervisor
组播和广播的知识点梳理
Qt TCP 分包粘包的解决方法
Pickle and savez_ Compressed compressed volume comparison
Realize a binary read-write address book
Unity Vector3. Use and calculation principle of reflect
js Array 列表 实战使用总结
Solution of QT TCP packet sticking
Sword finger offer II 039 Maximum rectangular area of histogram
Compilation and connection of shader in games202 webgl (learn from)
C# AES对字符串进行加密
[effective Objective-C] - memory management
UCF(暑期团队赛二)