In Java, how can I read data from multiple files using the generic character syntax posix?

advertisements

Currently, I have a script that loops over System.in for data processing. I am passing data to it from several files with cat.

cat myfiles*.txt | java MyDataProcessor

Based on the idea that cat adds some inefficiency vs. Java opening the files directly, I'd like to optimize this to where Java opens the files directly:

java MyDataProcessor myfiles*.txt

Are there any Java libraries that make this fairly easy (i.e. that handle the translation of posix wildcards into file handlers)?


Java 7 added a PathMatcher class that can be used to validate a path name based on a glob (which will be similar to the matching done by your shell)

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:myfiles*.txt");
matcher.matches(filename);

An example of walking a file tree and searching for files based on globs can be found in the Oracle Java tutorials here