* (i.e. the groupth group of the match)
* @throws IOException if the file could not be read.
*/
public static String[] findPattern(File file, Pattern pattern, int group) throws IOException {
Logger log = new ConsoleLogger();
ArrayList occurences = new ArrayList();
FileInputStream fis = null;
FileChannel fc = null;
try {
// Open the file and then get a channel from the stream
fis = new FileInputStream(file);
fc = fis.getChannel();
// Get the file's size and then map it into memory
int sz = (int)fc.size();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
// Decode the file into a char buffer
CharBuffer cb = decoder.decode(bb);
// Perform the search
Matcher pm = pattern.matcher(cb); // Pattern matcher
while (pm.find()) {
occurences.add(pm.group(group));
}
} catch (FileNotFoundException e) {
log.error("file not found " +e.toString());
} catch (CharacterCodingException e) {
log.error("encoding problem " +e.toString());
} catch (IOException e) {
log.error("IO exception" +e.toString());
} finally {
// Close the channel and the stream
if (fc != null)
fc.close();
if (fis != null)