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"

3 comments:

  1. Delight The informative approach maintained throughout SSO ID is truly impressive and commendable. Content is thoughtfully prepared, engaging, and highly beneficial for users. The platform consistently provides reliable information that supports understanding while maintaining excellent standards of quality and accessibility.

    ReplyDelete
  2. Keystone The impressive standards maintained by NewsAppear are clearly visible in every publication. Readers benefit from engaging articles, practical knowledge, and trustworthy information that deliver genuine value. The platform continues earning appreciation through professionalism, consistency, and informative reporting.

    ReplyDelete
  3. The quality of ryubing is impressive. The website offers a great balance between functionality and ease of use, making it enjoyable to visit.

    ReplyDelete