What you're likely seeing here is a result of the way you are collating result
. Remember that String
s in Java are immutable - therefore when string concatenation occurs, a new String
has to be instantiated, which can often involve copying all the data contained in that String
. You have the following code executing for every line:
result=result+inputLine+"\n";
Under the covers, this line involves:
- A new StringBuffer is created with the entire content of
result
so far inputLine
is appended to theStringBuffer
- The
StringBuffer
is converted to aString
- A new
StringBuffer
is created for thatString
- A newline character is appended to that
StringBuffer
- The
StringBuffer
is converted to aString
- That
String
is stored asresult
.
This operation will become more and more time-consuming as result
gets bigger and bigger - and your results appear to show (albeit from a sample of 2!) that the results increase drastically with page size.
Instead, use StringBuffer
directly.
StringBuffer buffer = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
buffer.append(inputLine).append('\n');
cicliLettura++;
}
String result = buffer.toString();