1

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?

StoreCode
  • 155
  • 11
  • 1
    What exactly do you want to do, check if the filename matches a pattern, or match against the content of the file? 1: Do `file.getName()`, 2: This is much more complicated, you must open and read the file and match against the content of the file - matcher isn't going to do this automatically for you. – Jesper Nov 30 '17 at 15:27
  • 1
    Obviously, a File isn't a CharSequence. You have to read the file first and then convert it to a String/CharSequence. https://stackoverflow.com/questions/16027229/reading-from-a-text-file-and-storing-in-a-string – Merch0 Nov 30 '17 at 15:28
  • @Jesper: I want to match the content of the file – StoreCode Nov 30 '17 at 15:31
  • So load the file's content, and pass that into `match`... – Jon Skeet Nov 30 '17 at 15:35

2 Answers2

3

Of course you cannot cast a File to CharSequence, they have nothing to do with each other.

Method matcher in the Pattern class accepts a CharSequence argument, so you need to pass a CharSequence (most likely a String) to it.

You need to read contents of the file. There is a lot of methods and it all depends if you know if your file is big or small. If it's small then you can just read all lines, collect them into a single String and pass it to the matcher method. If it's big then you can't read it all at once (you would consume a lot of memory) so you need to read it in chunks.

Considering that you need to look through the contents and find a specific pattern, this may be difficult - let's say your pattern is longer than a single chunk. Thus I suggest thinking more about the correct approach to this if your files are really big.

Check this for reading contents of a file: How do I create a Java string from the contents of a file?

Shadov
  • 5,421
  • 2
  • 19
  • 38
1

I modified the method in this manner:

 public void countWord(String word, File file) throws FileNotFoundException {
                int count = 0; 
                Scanner scanner = new Scanner(file);
                while (scanner.hasNextLine()) {
                String nextToken = scanner.next();

                Pattern pattern = Pattern.compile(word);
                java.util.regex.Matcher matcher = pattern.matcher(nextToken);

                while (matcher.find()) {
                count++;
                System.out.println("found: " + count + " : "
                                + matcher.start() + " - " + matcher.end());
                    }
                }

Thanks

StoreCode
  • 155
  • 11