The Following is a snapshot of my code , the input file is of size 45 Mb
Scanner fileScanner = new Scanner(file);
String scannedFarm;
try{
Pattern p = Pattern.compile("^(?:.+(?:\\r?\\n|\\Z)){2,}",Pattern.MULTILINE);
while((scannedFarm = fileScanner.findWithinHorizon(p, 0)) != null){ // Throws Exception
...
...
I’ll add any additional info that may clarify why the exception is thrown.
Related question which describes the file format may help: Java, Regular Expression HasNext starts with empty line, multi-platform support.
BTW: This works on small files like a charm, on larger it fails , does a String has maximum size returned from findWithinHorizon?
UPDATE
As was requested a small stack trace:
And a small example of the file snapshot:
I don’t know the workings of the
Scannerclass, but whatever.findwithinHorizon()does with the regex, that regex is extremely strange.This regex will match the entire file at once, as long as each line is at least one character long. If there are empty lines, then it will match all blocks in-between empty lines that span at least two lines. If that’s what you actually intend to do, there’s a much better way of doing that:
In order to avoid unnecessary backtracking of the regex engine, you could make all the quantifiers possessive:
With or without possessive quantifiers, this regex matches as follows: