Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created October 26, 2012 22:30
Show Gist options
  • Save kristopherjohnson/3961936 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3961936 to your computer and use it in GitHub Desktop.
Read string from InputStream
// http://stackoverflow.com/a/9949592/1175
String readStringFromStream(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();
}
String readStringFromStream(InputStream in, String charsetName) throws IOException {
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in, charsetName));
for(String line = br.readLine(); line != null; line = br.readLine())
out.append(line);
br.close();
return out.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment