Sri Sri - 3 months ago 7
Java Question

Java: Find .txt files in specified folder

Is there a built in Java code that will parse a given folder and search it for

.txt
files?

Answer

You can use the listFiles() method provided by the java.io.File class;

import java.io.File;
import java.io.FilenameFilter;

public class Filter {

    public File[] finder( String dirName){
    	File dir = new File(dirName);

    	return dir.listFiles(new FilenameFilter() { 
    	         public boolean accept(File dir, String filename)
    	              { return filename.endsWith(".txt"); }
    	} );

    }

}
Comments