io - Listing files in a directory matching a pattern in Java -


i'm looking way list of files match pattern (pref regex) in given directory.

i've found tutorial online uses apache's commons-io package following code:

collection getallfilesthatmatchfilenameextension(string directoryname, string extension) {   file directory = new file(directoryname);   return fileutils.listfiles(directory, new wildcardfilefilter(extension), null); } 

but returns base collection (according the docs it's collection of java.io.file). there way returns type safe generic collection?

see file#listfiles(filenamefilter).

file dir = new file("."); file [] files = dir.listfiles(new filenamefilter() {     @override     public boolean accept(file dir, string name) {         return name.endswith(".xml");     } });  (file xmlfile : files) {     system.out.println(xmlfile); } 

Comments

Popular posts from this blog

ruby - When to use an ORM (Sequel, Datamapper, AR, etc.) vs. pure SQL for querying -

php - PHPDoc: @return void necessary? -

c++ - Convert big endian to little endian when reading from a binary file -