When developing Java, I will inevitably encounter the need to convert input streams into String type. I often encounter such needs in Android development, so I will make this into a tool class and share it with you, hoping to help you. This is also my first time writing a personal blog, I hope you can support it. Thanks!
public static String streamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); //new A StringBuffer is used for string stitching StringBuffer sb = new StringBuffer(); String line = null; try { //When the input stream content is finished reading while ((line = reader.readLine()) != null) { sb.append(line + "/n"); } //Remember to close stream data to save memory consumption is.close(); reader.close(); return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null;}The above is the Java implementation of input streams into String introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!