Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Files.readAllLines - what constitutes a "large" file?

Tags:

java

The Files.readAllLines javadoc states:

"Note that this method is intended for simple cases where it is convenient to read all lines in a single operation. It is not intended for reading in large files."

What constitutes a large file in this context? (I'm also curious about why it is not suggested for large files. Is it the actual I/O that is the problem, or is it the size of memory used to store the results of the reading?)

like image 235
kc2001 Avatar asked Feb 04 '17 21:02

kc2001


1 Answers

The problem is that this method reads all contents of the text file into the memory. If it is too large, you will simply receive java.lang.OutOfMemoryError.

Yep, to clarify: large is a relative term. Its amount can not be specified as it depends on the free memory on the particular machine.

like image 135
Andremoniy Avatar answered Sep 30 '22 11:09

Andremoniy