SyntaxHighlighter JS

2013-05-29

Convert InputStream to String in two lines of code

Below is code to convert from java.io.InputStream into a String in just two line of Java 5 code.

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

}

Idea taken from "Stupid Scanner tricks"

No comments:

Post a Comment