I often use the Scanner class to read files because it is so convenient.
String inputFileName;
Scanner fileScanner;
inputFileName = "input.txt";
fileScanner = new Scanner (new File(inputFileName));
My question is, does the above statement load the entire file into memory at once? Or do subsequent calls on the fileScanner like
fileScanner.nextLine();
read from the file (i.e. from external storage and not from memory)? I ask because I am concerned about what might happen if the file is too huge to be read into memory all at once. Thanks.
If you read the source code you can answer the question yourself.
It appear that the implementation of the Scanner constructor in question shows:
Latter this is wrapped into a Reader:
And it is read using a buffer size
As you can see in the final constructor in the construction chain:
So, it appears scanner does not read the entire file at once.