In this example shows how to get specific files from a folder. Sometimes we need to pic only specific extensions from the given folder. Implement FilenameFilter class and override accept() method, and add your filter logic here. Pass this object to list() method to get specific file extensions.
Example:
package com.javatest.files; import java.io.File; import java.io.FilenameFilter; public class FileFiltertest { public static void main(String a[]){ File file = new File("C:/Myjava/"); String[] files = file.list(new FilenameFilter() { public boolean accept(File dir, String name) { if(name.toLowerCase().endsWith("abc.txt")){ return true; } else { return false; }}}); for(String f:files){ System.out.println(f); }}} |