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
Post a Comment