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

边栏推荐
- Vulhub vulnerability recurrence 67_ Supervisor
- 注释、接续、转义等符号
- [QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
- C AES encrypts strings
- [QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
- Some common skills on unity inspector are generally used for editor extension or others
- Improve jpopup to realize dynamic control disable
- Graduation design game mall
- Configuration file converted from Excel to Lua
- 【云原生】3.1 Kubernetes平台安装KubeSpher
猜你喜欢

Modbus protocol communication exception

SQLite add index

Simple understanding of interpreters and compilers

C Advanced - data storage (Part 1)

Modbus协议通信异常

Idea one key guide package

01. 开发博客项目之项目介绍

Talking about the type and function of lens filter

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

Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
随机推荐
Driver development - hellowdm driver
idea一键导包
【华为机试真题详解】检查是否存在满足条件的数字组合
05. 博客项目之安全
Mysql高级篇学习总结9:创建索引、删除索引、降序索引、隐藏索引
【torch】|torch. nn. utils. clip_ grad_ norm_
Fiddler installed the certificate, or prompted that the certificate is invalid
Safe mode on Windows
Please wait while Jenkins is getting ready to work
Pix2pix: image to image conversion using conditional countermeasure networks
Nacos TC setup of highly available Seata (02)
UCF(暑期团队赛二)
Configuration file converted from Excel to Lua
05. Security of blog project
Set detailed map + interview questions
03. 开发博客项目之登录
SQLite add index
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
Check the useful photo lossless magnification software on Apple computer
Compilation and connection of shader in games202 webgl (learn from)