当前位置:网站首页>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 :
边栏推荐
- 04. 项目博客之日志
- Fuzzy -- basic application method of AFL
- Promotion hung up! The leader said it wasn't my poor skills
- GAMES202-WebGL中shader的编译和连接(了解向)
- Fiddler installed the certificate, or prompted that the certificate is invalid
- jdbc使用call调用存储过程报错
- 【华为机试真题详解】检查是否存在满足条件的数字组合
- Talking about the type and function of lens filter
- Yyds dry inventory SSH Remote Connection introduction
- Figure database ongdb release v-1.0.3
猜你喜欢
[cloud native] 3.1 kubernetes platform installation kubespher
Modbus protocol communication exception
初识CDN
趋势前沿 | 达摩院语音 AI 最新技术大全
注释、接续、转义等符号
Fiddler installed the certificate, or prompted that the certificate is invalid
Review of double pointer problems
图数据库ONgDB Release v-1.0.3
Zoom and pan image in Photoshop 2022
GAMES202-WebGL中shader的編譯和連接(了解向)
随机推荐
[mask requirements of OSPF and Isis in multi access network]
Nacos - TC Construction of High available seata (02)
Three methods of Oracle two table Association update
SQLite queries the maximum value and returns the whole row of data
Easy to understand IIC protocol explanation
Some common skills on unity inspector are generally used for editor extension or others
用StopWatch 统计代码耗时
Mongodb basic knowledge summary
Mysql高级篇学习总结9:创建索引、删除索引、降序索引、隐藏索引
Rce code and Command Execution Vulnerability
Jvxetable用slot植入j-popup
Set detailed map + interview questions
Check the useful photo lossless magnification software on Apple computer
HAC cluster modifying administrator user password
Modbus协议通信异常
Vulhub vulnerability recurrence 68_ ThinkPHP
Fuzzy -- basic application method of AFL
Algorithm -- climbing stairs (kotlin)
Promotion hung up! The leader said it wasn't my poor skills
Summary of redis basic knowledge points