当前位置:网站首页>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 :

边栏推荐
猜你喜欢

Class inheritance in yyds dry inventory C

02. Develop data storage of blog project

剑指 Offer II 039. 直方图最大矩形面积

JS array list actual use summary

Principle and performance analysis of lepton lossless compression

自建DNS服务器,客户端打开网页慢,解决办法

Easy to understand IIC protocol explanation
![[leetcode16] the sum of the nearest three numbers (double pointer)](/img/99/a167b0fe2962dd0b5fccd2d9280052.jpg)
[leetcode16] the sum of the nearest three numbers (double pointer)
![[untitled]](/img/7e/d0724193f2f2c8681a68bda9e08289.jpg)
[untitled]

毕业设计游戏商城
随机推荐
JS array list actual use summary
C# AES对字符串进行加密
Codeforces Round #804 (Div. 2) Editorial(A-B)
UCF (2022 summer team competition I)
趋势前沿 | 达摩院语音 AI 最新技术大全
01. 开发博客项目之项目介绍
Pointer classic written test questions
[detailed explanation of Huawei machine test] statistics of shooting competition results
Jvxetable用slot植入j-popup
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
F12 solve the problem that web pages cannot be copied
Vulhub vulnerability recurrence 73_ Webmin
Mysql高级篇学习总结9:创建索引、删除索引、降序索引、隐藏索引
Vulhub vulnerability recurrence 72_ uWSGI
Principle and performance analysis of lepton lossless compression
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
[leetcode16] the sum of the nearest three numbers (double pointer)
ARTS Week 25
Summary of deep learning tuning tricks
Oracle query table index, unique constraint, field