01/07/2013

[Java] Convert InputStream to String

This code snippet is the work of Pavel Repin from StackOverflow, and it's highly useful if work in Java. It allows you to easily convert an InputStream to a String taking advantage of the Scanner class, plus, since it's a native Java class, you don't need to download additional jars:

 public static String convertStreamToString(java.io.InputStream is) {  
   java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");  
   return s.hasNext() ? s.next() : "";  
 }  

No comments:

Post a Comment

With great power comes great responsibility.

Da grandi poteri derivano grandi responsabilità.