Possible Duplicates:
How do I convert an InputStream to a String in Java?
In Java how do a read an input stream in to a string?
I have an InputSteam
and need to simply get a single simple String
with the complete contents.
How is this done in Java?
-
2Can you elaborate? By definition, a Stream is unbounded. Unless there are some more constraints, you can't get a String (something of fixed size) from an unbounded stream.Thomas Owens– Thomas Owens2010年08月13日 17:05:48 +00:00Commented Aug 13, 2010 at 17:05
-
2This has been asked many times: stackoverflow.com/questions/1763789/…bakkal– bakkal2010年08月13日 17:05:54 +00:00Commented Aug 13, 2010 at 17:05
-
stackoverflow.com/questions/309424/…Patrick Kafka– Patrick Kafka2010年08月13日 17:07:00 +00:00Commented Aug 13, 2010 at 17:07
-
How to do it with nio FileChannel: stackoverflow.com/questions/326390x4u– x4u2011年06月04日 11:08:11 +00:00Commented Jun 4, 2011 at 11:08
-
Awesome solution here: Read/convert an InputStream to a StringRichard Le Mesurier– Richard Le Mesurier2013年02月15日 11:40:33 +00:00Commented Feb 15, 2013 at 11:40
6 Answers 6
Here is a modification of Gopi's answer that doesn't have the line ending problem and is also more effective as it doesn't need temporary String objects for every line and avoids the redundant copying in BufferedReader and the extra work in readLine().
public static String convertStreamToString( InputStream is, String ecoding ) throws IOException
{
StringBuilder sb = new StringBuilder( Math.max( 16, is.available() ) );
char[] tmp = new char[ 4096 ];
try {
InputStreamReader reader = new InputStreamReader( is, ecoding );
for( int cnt; ( cnt = reader.read( tmp ) ) > 0; )
sb.append( tmp, 0, cnt );
} finally {
is.close();
}
return sb.toString();
}
-
1+1 more sane encoding handling.tc.– tc.2010年08月13日 17:48:47 +00:00Commented Aug 13, 2010 at 17:48
You need to construct an InputStreamReader
to wrap the input stream, converting between binary data and text. Specify the appropriate encoding based on your input source.
Once you've got an InputStreamReader
, you could create a BufferedReader
and read the contents line by line, or just read buffer-by-buffer and append to a StringBuilder
until the read()
call returns -1.
The Guava library makes the second part of this easy - use CharStreams.toString(inputStreamReader)
.
-
I've never tried this, but shouldn't you be aware of what the stream represents before you do this? If you were to have a stream such that read() would never return -1, there would be no value in even trying this. But, at the same time, I can't think of a stream off the top of my head that never ends.Thomas Owens– Thomas Owens2010年08月13日 17:21:22 +00:00Commented Aug 13, 2010 at 17:21
-
1@Thomas: Well, you could easily create a stream which only ever gave random numbers. I was assuming that the question was asked in a situation where the concept of "read to the end of the stream" did actually make sense...Jon Skeet– Jon Skeet2010年08月13日 17:33:26 +00:00Commented Aug 13, 2010 at 17:33
Here is an example code adapted from here.
public String convertStreamToString(InputStream is) throws IOException {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
}
-
2Note that that will normalize line endings - it won't necessarily return the exact original text data.Jon Skeet– Jon Skeet2010年08月13日 17:11:17 +00:00Commented Aug 13, 2010 at 17:11
-
true. edited code to incorporate your suggestion.Gopi– Gopi2010年08月13日 17:16:13 +00:00Commented Aug 13, 2010 at 17:16
-
Now it won't include the line endings at all. I think Jon Skeet was just saying that every line ending
\r\n
(or\r
) becomes\n
.Justin Ardini– Justin Ardini2010年08月13日 17:19:28 +00:00Commented Aug 13, 2010 at 17:19 -
aaah. thanks for bringing it to notice. reverted.Gopi– Gopi2010年08月13日 17:25:22 +00:00Commented Aug 13, 2010 at 17:25
You can also use Apache Commons IO library
Specifically, you can use IOUtils#toString(InputStream inputStream) method
-
As a software engineer, I love this. Reusing existing libraries to get the job done. Apache Commons (and all of the Apache projects in general) are very well done and quite helpful in a number of situations.Thomas Owens– Thomas Owens2010年08月13日 18:03:57 +00:00Commented Aug 13, 2010 at 18:03
-
Note that using the method above uses the platforms default encoding, i.e. the result depends on user settings or other external factors. Most of the time it is better to use a method expecting the encoding as additional parameter. See this comment on a related question. Apaches
IOUtils
provides such methods, too. BTW, the link to Javadoc is broken...siegi– siegi2014年12月06日 10:42:49 +00:00Commented Dec 6, 2014 at 10:42
You could also use a StringWriter as follows; each read
from your InputStream is matched with a write
(or append
) to the StringWriter, and upon completion you can call getBuffer
to get a StringBuffer which could be used directly or you could get call its toString
method.
Wrap the Stream in a Reader to get locale conversion, and then keep reading while collecting in a StringBuffer. When done, do a toString() on the StringBuffer.