dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Convert an InputStream into a String

How to use Apache Commons IOUtils to copy the InputStream into a StringWriter.


advertisement

A quick approach relies on Apache Commons IOUtils to copy the InputStream into a StringWriter:

StringWriter sw = new StringWriter();
IOUtils.copy(inputStream, sw, encoding);
String result = sw.toString();

Or, plain JDK via Scanner:

Scanner scanner = new Scanner(inputStream).useDelimiter("\\A");
String result = scanner.hasNext() ? scanner.next() : "";


   
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date