InputStream与String,Byte之间互转

  1. 本文将介绍InputStream与String,Byte之间的相互转换。以代码来说明:

  2. import java.io.ByteArrayInputStream;  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. /**  
  8.  *   
  9.  * @author Andy.Chen  
  10.  * @mail Chenjunjun.ZJ@gmail.com  
  11.  *  
  12.  */  
  13. public class InputStreamUtils {  
  14.       
  15.     final static int BUFFER_SIZE = 4096;  
  16.       
  17.     /**  
  18.      * 将InputStream转换成String  
  19.      * @param in InputStream  
  20.      * @return String  
  21.      * @throws Exception  
  22.      *   
  23.      */  
  24.     public static String InputStreamTOString(InputStream in) throws Exception{  
  25.           
  26.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  27.         byte[] data = new byte[BUFFER_SIZE];  
  28.         int count = -1;  
  29.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  30.             outStream.write(data, 0, count);  
  31.           
  32.         data = null;  
  33.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  34.     }  
  35.       
  36.     /**  
  37.      * 将InputStream转换成某种字符编码的String  
  38.      * @param in  
  39.      * @param encoding  
  40.      * @return  
  41.      * @throws Exception  
  42.      */  
  43.          public static String InputStreamTOString(InputStream in,String encoding) throws Exception{  
  44.           
  45.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  46.         byte[] data = new byte[BUFFER_SIZE];  
  47.         int count = -1;  
  48.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  49.             outStream.write(data, 0, count);  
  50.           
  51.         data = null;  
  52.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  53.     }  
  54.       
  55.     /**  
  56.      * 将String转换成InputStream  
  57.      * @param in  
  58.      * @return  
  59.      * @throws Exception  
  60.      */  
  61.     public static InputStream StringTOInputStream(String in) throws Exception{  
  62.           
  63.         ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));  
  64.         return is;  
  65.     }  
  66.       
  67.     /**  
  68.      * 将InputStream转换成byte数组  
  69.      * @param in InputStream  
  70.      * @return byte[]  
  71.      * @throws IOException  
  72.      */  
  73.     public static byte[] InputStreamTOByte(InputStream in) throws IOException{  
  74.           
  75.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  76.         byte[] data = new byte[BUFFER_SIZE];  
  77.         int count = -1;  
  78.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  79.             outStream.write(data, 0, count);  
  80.           
  81.         data = null;  
  82.         return outStream.toByteArray();  
  83.     }  
  84.       
  85.     /**  
  86.      * 将byte数组转换成InputStream  
  87.      * @param in  
  88.      * @return  
  89.      * @throws Exception  
  90.      */  
  91.     public static InputStream byteTOInputStream(byte[] in) throws Exception{  
  92.           
  93.         ByteArrayInputStream is = new ByteArrayInputStream(in);  
  94.         return is;  
  95.     }  
  96.       
  97.     /**  
  98.      * 将byte数组转换成String  
  99.      * @param in  
  100.      * @return  
  101.      * @throws Exception  
  102.      */  
  103.     public static String byteTOString(byte[] in) throws Exception{  
  104.           
  105.         InputStream is = byteTOInputStream(in);  
  106.         return InputStreamTOString(is);  
  107.     }  
  108.   
  109. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值