I have this simple method that uses Java Matcher
public int countWord(String word, File file) throws FileNotFoundException {
String patternString = word;
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(file);
int count = 0;
while (matcher.find()) {
count++;
System.out.println("found: " + count + " : "
+ matcher.start() + " - " + matcher.end());
}
return count;
}
My idea is to pass a file into the instruction:
Matcher matcher = pattern.matcher(file);
but Java
complains it even if I follow the advice of the IDE that said to do a cast like this:
java.util.regex.Matcher matcher = pattern.matcher((CharSequence) file);
In fact when I try to launch the compilation it reports this message:
Exception in thread "main" java.lang.ClassCastException: java.io.File cannot be cast to java.lang.CharSequence
How can I pass this obstacle?