I want to directly read a file, put it into a string without storing the file locally. I used to do this with an old project, but I don’t have the source code anymore. I used to be able to get the source of my website this way.
However, I don’t remember if I did it by “InputStream to String array of lines to String”, or if I directly read it into a String.
Was there a function for this, or am I remembering wrong?
(Note: this function would be the PHP equivalent of file_get_contents($path))
You need to use
InputStreamReaderto convert from a binary input stream to aReaderwhich is appropriate for reading text.After that, you need to read to the end of the reader.
Personally I’d do all this with Guava, which has convenience methods for this sort of thing, e.g.
CharStreams.toString(Readable).When you create the
InputStreamReader, make sure you supply the appropriate character encoding – if you don’t, you’ll get junk text out (just like trying to play an MP3 file as if it were a WAV, for example).