I am trying to read a simple text file into a String. Of course there is the usual way of getting the input stream and iterating with readLine() and reading contents into String.

Having done this hundreds of times in past, I just wondered how can I do this in minimum lines of code? Isn’t there something in java like String fileContents = XXX.readFile(myFile/*File*/) .. rather anything that looks as simple as this?

I know there are libraries like Apache Commons IO which provide such simplifications or even I can write a simple Util class to do this. But all that I wonder is – this is a so frequent operation that everyone needs then why doesn’t Java provide such simple function? Isn’t there really a single method somewhere to read a file into string with some default or specified encoding?

9 Answers
9

Leave a Reply

Your email address will not be published. Required fields are marked *