Will java readLine () be able to read a 1GB line

BufferedReader # readLine () tries to fill its buffer with 8192 bytes. If the size of the line itself is much larger than it will throw an exception or read it in chunks and print the entire line?

+3


source to share


1 answer


BufferedReader.readLine()

a return is announced that String

can be 1 GB in length. A String

is char[]

internally represented, and the length of an array in Java can be much larger than 10 ^ 9 (the maximum size of an array is slightly less Integer.MAX_VALUE

).

The implementation BufferedReader.readLine()

uses StringBuffer

which also creates String

in an array char

, so it also doesn't prevent the line length from being 1 GB.

The internal buffer BufferedReader

does not limit the maximum row size, it is simply used to speed up certain operations and support Reader.mark()

and Reader.reset()

.



Note:

In your favor, String

stores characters not bytes, so the array limit refers to the number of characters, which can be even larger than the number of bytes depending on the character encoding used and the actual text content (for example, if UTF-8 encoding is used the character might even be encoded up to 4 bytes of data).

+6


source







All Articles