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?

Community
  • 1
  • 1
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • 4
    "Why did they not just add this to the JDK in the first place?" Are you hoping one of the JDK authors will answer this question? Or are you venting about something you don't like? – S.Lott Aug 18 '09 at 18:21
  • 4
    There's no end of "why isn't X in the JRE" questions, none of which are answerable. – skaffman Aug 18 '09 at 18:21
  • 1
    But if it would have been harmful for reason that is not obvious, it would be interesting (and educational) to know why. – finnw Aug 18 '09 at 18:34
  • 1
    In programming, having a standard 10 tools that you can combine to do various jobs beats the hell out of having those 10 tools plus 1500 specific ones prebuilt out of the 10. If you can do it in under 5 lines of code, just do it and leave libraries out of the picture (or write your own). Java is not a scripting language, if it was then having everything in the standard libraries would make sense because you aren't really creating an entire app, you're creating a script where having to write your own subroutines is a larger portion of your code. – Bill K Aug 18 '09 at 19:15
  • 1
    @S Lott: if you look at my question you will see that I at no point say that Sun were wrong not not to include it in the JDK. I was merely wanting to know what people thought the reasons for the omission were! I'm pretty annoyed this has been closed: it's a genuine question and it's programming related. – oxbow_lakes Aug 18 '09 at 20:46
  • 1
    @skaffman: except possibly this one. All is required as an answer is an opinion on why it's not in there. But well done for the assumption that noone *else* could answer it! – oxbow_lakes Aug 18 '09 at 20:50
  • `String wholeFile = new Scanner(pathToFile).useDeliminter("\\Z").next();` – jjnguy Aug 20 '09 at 17:33

2 Answers2

3

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
  • 12,285
  • 10
  • 54
  • 78
  • 5
    System.exit() crashes VMs left and right, but that's in the JRE. Proper documentation is the key. – skaffman Aug 18 '09 at 18:30
  • 2
    Plus, methods like this exist in lots of other languages such as Python and C#. – Eli Courtwright Aug 18 '09 at 18:42
  • 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 Aug 18 '09 at 19:42
2

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

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95