How to list files from folder by adding filters in Java

In day to day requirement, we normally check files in a folder . For example the debugging purpose will check log files, Suppose, we want to list files by adding filter it will be useful .

The following example expalins , how to list files from folder by adding our custom filter.


package com.onlinecodegeek.javase;
import java.io.File;
import java.io.FilenameFilter;

public class ListFilesByFilter {

public static void main(String a[]){
File file = new File("C:/Temp/");
String[] files = file.list(new FilenameFilter() {

@Override
public boolean accept(File dir, String name) {
if(name.toLowerCase().endsWith(".log")){
return true;
} else {
return false;
}
}
});
for(String f:files){
System.out.println(f);
}
}
}
OUTPUT

jre7_x64_setup.log
jre7_x86_setup.log

You can also check :How to find list of files in a folder using java

SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment