[]19.inputstream and String,byte transfer [reprint]

Source: Internet
Author: User

Import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.InputStream; /** * @see
InputStream Conversion to String
Convert String to InputStream
InputStream conversion to byte array
Convert byte to InputStream
Convert byte to String * @author Mr. Fat * @mail [email protected] * */public class Inputstreamutils {final static int buffer_size = 4096; /** * Convert InputStream to String * @param in InputStream * @return String * @throws Exception * */ public static String inputstreamtostring (InputStream in) throws exception{Bytearrayoutputstream OutStream = new Bytearrayoutputstream (); byte[] data = new Byte[buffer_size]; int count =-1; while ((count = In.read (data,0,buffer_size))! =-1) outstream.write (data, 0, count); data = null; return new String (Outstream.tobytearray (), "iso-8859-1"); }/** * Convert InputStream to a character encoded string * @param in * @param encoding * @return * @throws Excepti On */public static String inputstreamtostring (InputStream in,string encoding) throws exception{ Bytearrayoutputstream OutStream = new BytearrayoutputstreAM (); byte[] data = new Byte[buffer_size]; int count =-1; while ((count = In.read (data,0,buffer_size))! =-1) outstream.write (data, 0, count); data = null; return new String (Outstream.tobytearray (), "iso-8859-1"); /** * Convert String to InputStream * @param in * @return * @throws Exception */public static in Putstream Stringtoinputstream (String in) throws exception{Bytearrayinputstream is = new Bytearrayinputstr EAM (In.getbytes ("iso-8859-1")); return is; }/** * Convert InputStream to byte array * @param in InputStream * @return byte[] * @throws IOException * /public static byte[] Inputstreamtobyte (InputStream in) throws ioexception{Bytearrayoutputstream outs Tream = new Bytearrayoutputstream (); byte[] data = new Byte[buffer_size]; int count =-1; while ((count = In.read (data,0,buffer_size))! =-1) outstream.write(data, 0, count); data = null; return Outstream.tobytearray (); /** * Converts a byte array to InputStream * @param in * @return * @throws Exception */public static in Putstream Bytetoinputstream (byte[] in) throws exception{Bytearrayinputstream is = new Bytearrayinputstrea M (in); return is; /** * Convert byte array to String * @param in * @return * @throws Exception */public static String Bytetostring (byte[] in) throws exception{InputStream are = Bytetoinputstream (in); Return inputstreamtostring (IS); } }

  

[]19.inputstream and String,byte transfer [reprint]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.