Reputation: 219
I am confused with the term "Do not read file into memory all at once". For the below code, will it be correct to say that I not reading file into memory all at once but line by line?
File file = new File("C:\\Users\\Desktop\\test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null)
System.out.println(st);
}
}
Upvotes: 2
Views: 1129
Reputation: 123650
For the below code, will it be correct to say that I not reading file into memory all at once but line by line?
Yes, this is correct. You are not falling into the anti-pattern of reading files into memory in this code.
Your code allows you to process arbitrarily large files using a small and bounded amount of memory, which is the point of the guideline.
Upvotes: 0
Reputation: 285430
For the below code, will it be correct to say that I not reading file into memory all at once but line by line?
You cannot make any such assumptions. Since you are using a BufferedReader, all you can say with assuredness is that you are reading some of the file into a memory buffer, perhaps all of the file, but the buffer is in control, not you. And when the buffer's data has been depleted, then more of the file will be read into it, if there is still data remaining to be read.
Upvotes: 2