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
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:
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 aMatcher
matches against aCharSequence
, not aString
. But this package needs some love.One example returning a
List
of matching strings in a file can be: