Commons-IO has an IOUtils.toString(inputStream)
method, which can read all content from an input stream:
InputStream input = getInputStream();
String content = IOUtils.toString(input);
My question is shall I close the input stream manually after using it?
I thought IOUtils may close it since it has read all the content, but I can't find that in the source code.
You do need to close the input Stream, because the stream returned by the method you mention is actually FileInputStream or some other subclass of InputStream that holds a handle for a file. If you do not close this stream you have resource leakage.
skips() - skips and discards the specified number of bytes from the input stream. close() - closes the input stream.
Copies chars from a Reader to bytes on an OutputStream using the specified character encoding, and calling flush. Copies chars from a Reader to a Writer .
The javadoc says:
Wherever possible, the methods in this class do not flush or close the stream. This is to avoid making non-portable assumptions about the streams' origin and further use. Thus the caller is still responsible for closing streams after use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With