tags:

views:

202

answers:

2

In the comments to my answer about reading an entire file into memory using scala.io.Source, I asserted that the reason there is no read-whole-file-into-String method in Java is that it is not (of course) scalable with regards to how big the file is and how much heap you have.

However, I guess everyone has some method like:

String contents = IOUtil.readFile(f, "utf-8");

And the implementation of the method is just a few lines of code. Why did they not just add this to the JDK in the first place? Is it my (scalability) reason, or is there some other reason it was omitted?

+2  A: 

I think your own answer is right. What if your file is like 1GB long?

klez
+3  A: 

Because files can be an arbitrary size, and it's just bad programming practice to do that sort of thing without a lot of checks and balances to make sure you're not crashing VM's left and right.

MattC
System.exit() crashes VMs left and right, but that's in the JRE. Proper documentation is the key.
skaffman
Plus, methods like this exist in lots of other languages such as Python and C#.
Eli Courtwright
I guess I'm happier with the JDK forcing programmers to (presumably) think the process through instead of "Here, call this and trash all of your memory!"
MattC