0
✓
Quick and easy:
String result = (String)new ObjectInputStream( inputStream ).readObject();
0
InputStream IS=new URL("http://www.petrol.si/api/gas_prices.json").openStream();
ByteArrayOutputStream BAOS=new ByteArrayOutputStream();
IOUtils.copy(IS, BAOS);
String d= new String(BAOS.toByteArray(),"UTF-8");
System.out.println(d);
0
Apache Commons allows:
String myString = IOUtils.toString(myInputStream, "UTF-8");
Of course, you could choose other character encodings besides UTF-8.
Also see:
http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html#toString(java.io.InputStream, java.lang.String)
0
Taking into account file one should first get a java.io.Reader instance. This can then be read and added to a StringBuilder (we don't need StringBuffer if we are not accessing it in multiple threads, and StringBuilder is faster). The trick here is that we work in blocks, and as such don't need other buffering streams. The block size is parameterized for run-time performance optimization.
public static String slurp(final InputStream is, final int bufferSize)
{
final char[] buffer = new char[bufferSize];
final StringBuilder out = new StringBuilder();
try {
final Reader in = new InputStreamReader(is, "UTF-8");
try {
for (;;) {
int rsz = in.read(buffer, 0, buffer.length);
if (rsz < 0)
break;
out.append(buffer, 0, rsz);
}
}
finally {
in.close();
}
}
catch (UnsupportedEncodingException ex) {
/* ... */
}
catch (IOException ex) {
/* ... */
}
return out.toString();
}
0
How about this?
InputStream in = /* your InputStream */;
InputStreamReader is = new InputStreamReader(in);
StringBuilder sb=new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();
while(read != null) {
//System.out.println(read);
sb.append(read);
read =br.readLine();
}
return sb.toString();
0
If you are using Google-Collections/Guava you could do the following:
InputStream stream = ...
String content = CharStreams.toString(new InputStreamReader(stream, Charsets.UTF_8));
Closeables.closeQuietly(stream);
Note that the second parameter (i.e. Charsets.UTF_8) for the InputStreamReader isn't necessary, but it is generally a good idea to specify the encoding if you know it (which you should!)
0
public String readFully(InputStream inputStream, String encoding)
throws IOException {
return new String(readFully(inputStream), encoding);
}
private byte[] readFully(InputStream inputStream)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
return baos.toByteArray();
}
0
Here's the most elegant, pure-Java (no library) solution I came up with after some experimentation:
public static String fromStream(InputStream in) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
}
return out.toString();
}
0
How about:
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
public static String readInputStreamAsString(InputStream in)
throws IOException {
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
0
As an alternative to the Commons libraries, Google's excellent guava-libraries let you do this fairly concisely; given an InputStream named inputStream:
import com.google.common.io.CharStreams;
CharStreams.toString( new InputStreamReader( inputStream ));
0
Here's more-or-less sampath's answer, cleaned up a bit and represented as a function:
String streamToString(InputStream in) throws IOException {
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
for(String line = br.readLine(); line != null; line = br.readLine())
out.append(line);
br.close();
return out.toString();
}
0
Here's a way using only standard Java library (note that the stream is not closed, YMMV).
static String convertStreamToString(java.io.InputStream is) {
java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
I learned this trick from "Stupid Scanner tricks" article. The reason it works is because Scanner iterates over tokens in the stream, and in this case we separate tokens using "beginning of the input boundary" (\A) thus giving us only one token for the entire contents of the stream.
Note, if you need to be specific about the input stream's encoding, you can provide the second argument to Scanner constructor that indicates what charset to use (e.g. "UTF-8").
Hat tip goes also to Jacob, who once pointed me to the said article.
EDITED: Thanks to a suggestion from Patrick, made the function more robust when handling an empty input stream. One more edit: nixed try/catch, Patrick's way is more laconic.
Created at: 2013-11-13 17:07:08 UTC
By Erwin Von
0
A nice way to do this is using Apache commons IOUtils to copy the InputStream into a StringWriter... something like
StringWriter writer = new StringWriter();
IOUtils.copy(inputStream, writer, encoding);
String theString = writer.toString();
Alternatively, you could use ByteArrayOutputStream if you don't want to mix your Streams and Writers
0
If you can't use Commons IO (FileUtils/IOUtils/CopyUtils) here's an example using a BufferedReader to read the file line by line:
public class StringFromFile {
public static void main(String[] args) /*throws UnsupportedEncodingException*/ {
InputStream is = StringFromFile.class.getResourceAsStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is/*, "UTF-8"*/));
final int CHARS_PER_PAGE = 5000; //counting spaces
StringBuilder builder = new StringBuilder(CHARS_PER_PAGE);
try {
for(String line=br.readLine(); line!=null; line=br.readLine()) {
builder.append(line);
builder.append('\n');
}
} catch (IOException ignore) { }
String text = builder.toString();
System.out.println(text);
}
}
or if you want raw speed I'd propose a variation on what Paul de Vrieze suggested (which avoids using a StringWriter (which uses a StringBuffer internally) :
public class StringFromFileFast {
public static void main(String[] args) /*throws UnsupportedEncodingException*/ {
InputStream is = StringFromFileFast.class.getResourceAsStream("file.txt");
InputStreamReader input = new InputStreamReader(is/*, "UTF-8"*/);
final int CHARS_PER_PAGE = 5000; //counting spaces
final char[] buffer = new char[CHARS_PER_PAGE];
StringBuilder output = new StringBuilder(CHARS_PER_PAGE);
try {
for(int read = input.read(buffer, 0, buffer.length);
read != -1;
read = input.read(buffer, 0, buffer.length)) {
output.append(buffer, 0, read);
}
} catch (IOException ignore) { }
String text = output.toString();
System.out.println(text);
}
}
0
If you were feeling adventurous, you could mix Scala and Java and end up with this:
scala.io.Source.fromInputStream(is).mkString("")
Mixing Java and Scala code and libraries has it's benefits.
See full description here: Idiomatic way to convert an InputStream to a String in Scala
0
I ran some timing tests because time matters, always.
I attempted to get the response into a String 3 different ways. (shown below)
I left out try/catch blocks for the sake readability.
To give context, this is the preceding code for all 3 approaches:
String response;String url = "www.blah.com/path?key=value";GetMethod method = new GetMethod(url);int status = client.executeMethod(method);
1)
response = method.getResponseBodyAsString();
2)
InputStream resp = method.getResponseBodyAsStream();InputStreamReader is=new InputStreamReader(resp);BufferedReader br=new BufferedReader(is);String read = null;StringBuffer sb = new StringBuffer(read);while((read = br.readLine()) != null) {sb.append(read);}response = sb.toString();
3)InputStream iStream = method.getResponseBodyAsStream();StringWriter writer = new StringWriter();IOUtils.copy(iStream, writer, "UTF-8");response = writer.toString();
So, after running 500 tests on each approach with the same request/response data, here are the numbers. Once again, these are my findings and your findings may not be exactly the same, but I wrote this to give some indication to others of the efficiency differences of these approaches.
Ranks:
Approach #1
Approach #3 - 2.6% slower than #1
Approach #2 - 4.3% slower than #1
Any of these approaches is an appropriate solution to the grabbing a response and creating a String from it.
0
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
...
InputStream is = ....
ByteArrayOutputStream baos = new ByteArrayOutputStream(8192);
byte[] buffer = new byte[8192];
int count = 0;
try {
while ((count = is.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
}
finally {
try {
is.close();
}
catch (Exception ignore) {
}
}
String charset = "UTF-8";
String inputStreamAsString = baos.toString(charset);
0
InputStreamReader i=new InputStreamReader(s);
BufferedReader str=new BufferedReader(i);
String msg=str.readLine();
System.out.println(msg);
here s is your InputStream object which will get convert into String :)
0
make sure to close the streams at end if you use Stream Readers
private String readStream(InputStream iStream) throws IOException {
//build a Stream Reader, it can read char by char
InputStreamReader iStreamReader = new InputStreamReader(iStream);
//build a buffered Reader, so that i can read whole line at once
BufferedReader bReader = new BufferedReader(iStreamReader);
String line = null;
StringBuilder builder = new StringBuilder();
while((line = bReader.readLine()) != null) { //Read till end
builder.append(line);
}
bReader.close(); //close all opened stuff
iStreamReader.close();
iStream.close();
return builder.toString();
}
0
Well you can program it for yourself.. it's not complicated..
String Inputstream2String (InputStream is) throws IOException
{
final int PKG_SIZE = 1024;
byte[] data = new byte [PKG_SIZE];
StringBuilder buffer = new StringBuilder(PKG_SIZE * 10);
int size;
size = is.read(data, 0, data.length);
while (size > 0)
{
String str = new String(data, 0, size);
buffer.append(str);
size = is.read(data, 0, data.length);
}
return buffer.toString();
}
0
The below code worked for me.
URL url = MyClass.class.getResource("/" + configFileName);
BufferedInputStream bi = (BufferedInputStream) url.getContent();
byte[] buffer = new byte[bi.available() ];
int bytesRead = bi.read(buffer);
String out = new String(buffer);
Please note, according to Java docs, the available() method might not work with InputStream but always works with BufferedInputStream.
In case you don't want to use available() method we can always use the below code
URL url = MyClass.class.getResource("/" + configFileName);
BufferedInputStream bi = (BufferedInputStream) url.getContent();
File f = new File(url.getPath());
byte[] buffer = new byte[ (int) f.length()];
int bytesRead = bi.read(buffer);
String out = new String(buffer);
I am not sure if there will be any encoding issues. Please comment, if there will be any issues with the code
0
First ,you have to know the encoding of string that you want to convert.Because the java.io.InputStream operates an underlying array of bytes,however,a string is composed by a array of character that needs an encoding, e,g. UTF-8,the JDK will take the default encoding that is taken from System.getProperty("file.encoding","UTF-8");
byte[] bytes=new byte[inputStream.available()];
inputStream.read(bytes);
String s = new String(bytes);
If inputStream's byte array is very big, you could do it in loop.
:EOF