0

I want to find a special charsequence in a file and I want to read the whole line where the occurrences are.

The following code just checks the first line and fetchess this ( the first ) line. How can I fix it?

   Scanner scanner = new Scanner(file);
   String output = "";

    output = output + scanner.findInLine(pattern) + scanner.next();

pattern and file are parameters

rootBoy
  • 1
  • 1
  • 1
  • And what do you want to do with the result? Collect them in a list, something else? – fge Aug 20 '16 at 12:13

1 Answers1

0

UPDATED ANSWER according to the comments on this very answer

In fact, what is used is Scanner#findWithHorizon, which in fact calls the Pattern#compile method with a set of flags (Pattern#compile(String, int)).

The result seems to be applying this pattern over and over again in the input text over lines of a file; and this supposes of course that a pattern cannot match multiple lines at once.

Therefore:

public static final String findInFile(final Path file, final String pattern, 
    final int flags)
    throws IOException
{
    final StringBuilder sb = new StringBuilder();
    final Pattern p = Pattern.compile(pattern, flags);

    String line;
    Matcher m;

    try (
        final BufferedReader br = Files.newBufferedReader(path);
    ) {
        while ((line = br.readLine()) != null) {
            m = p.matcher(line);
            while (m.find())
                sb.append(m.group());
        }
    }

    return sb.toString();
}

For completeness I should add that I have developed some time ago a package which allows a text file of arbitrary length to be read as a CharSequence and which can be used to great effect here: https://github.com/fge/largetext. It would work beautifully here since a Matcher matches against a CharSequence, not a String. But this package needs some love.


One example returning a List of matching strings in a file can be:

private static List<String> findLines(final Path path, final String pattern)
    throws IOException
{
    final Predicate<String> predicate = Pattern.compile(pattern).asPredicate();

    try (
        final Stream<String> stream = Files.lines(path);
    ) {
        return stream.filter(predicate).collect(Collectors.toList());
    }
}
fge
  • 110,072
  • 26
  • 223
  • 312
  • Well, uh, if you don't define your problem more precisely, there is little with can be done – fge Aug 20 '16 at 17:01
  • yes, sorry! But i didnt know how I can explain my problem. – rootBoy Aug 20 '16 at 17:33
  • This is an effort you should commit to when asking a question :) Step back, think about the problem again, provide your own solution, explain why this solution does not do what you want... This is the secret of a good question :) – fge Aug 20 '16 at 17:49