java中byte,String,InputStream之间的转换

本文介绍Java中byte、String及InputStream间的相互转换方法。包括InputStream转String、byte数组,String转InputStream,byte数组转String等实用函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://zhoujingxian.iteye.com/blog/1682480

java中byte,String,InputStream之间的转换

  • 博客分类: 
  • J2SE
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值