private static int countWildcardPhraseFrequencies ( File dir , String pattern ) throws Exception { File [ ] files = dir . listFiles ( ) ; int total = 0 ; for ( int i = 0 ; i < files . length ; i ++ ) { File f = files [ i ] ; if ( f . isDirectory ( ) ) { total += countWildcardPhraseFrequencies ( f , pattern ) ; } else if ( f . getName ( ) . endsWith ( ".txt" ) ) { Scanner sc = new Scanner ( f ) ; while ( sc . hasNext ( ) ) { String line = sc . nextLine ( ) ; if ( line . matches ( pattern ) ) { total ++ ; } } } } return total ; }
Searches through all the . txt files in a directory and returns the total number of occurrences of a pattern .