当前位置:网站首页>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 :
边栏推荐
猜你喜欢
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
Implementing fuzzy query with dataframe
GAMES202-WebGL中shader的编译和连接(了解向)
Ora-01779: the column corresponding to the non key value saving table cannot be modified
Questions d'examen écrit classiques du pointeur
pix2pix:使用条件对抗网络的图像到图像转换
Simple understanding of interpreters and compilers
Codeforces Round #804 (Div. 2) Editorial(A-B)
Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?
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
随机推荐
Hyperledger Fabric2. Some basic concepts of X (1)
In 2022, we must enter the big factory as soon as possible
SQLite add index
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Leetcode dynamic planning day 16
03. Login of development blog project
[effective Objective-C] - memory management
[untitled]
Fiddler installed the certificate, or prompted that the certificate is invalid
[leetcode] 18. Sum of four numbers
Summary of redis basic knowledge points
Fuzzy -- basic application method of AFL
Vulhub vulnerability recurrence 68_ ThinkPHP
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
F12 solve the problem that web pages cannot be copied
2022 half year summary
idea一键导包
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
February 12 relativelayout
TCP three handshakes you need to know