athrunwang

          紀(jì)元
          <2012年1月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          統(tǒng)計(jì)

          • 隨筆 - 72
          • 文章 - 0
          • 評論 - 15
          • 引用 - 0

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          InputStream與String,Byte之間互轉(zhuǎn)
          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.io.InputStream;

          /**
           *
           * @author Andy.Chen
           * @mail Chenjunjun.ZJ@gmail.com
           *
           */
          public class InputStreamUtils {
              
              final static int BUFFER_SIZE = 4096;
              
              /**
               * 將InputStream轉(zhuǎn)換成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");
              }
              
              /**
               * 將InputStream轉(zhuǎn)換成某種字符編碼的String
               * @param in
               * @param encoding
               * @return
               * @throws Exception
               */
                   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");
              }
              
              /**
               * 將String轉(zhuǎn)換成InputStream
               * @param in
               * @return
               * @throws Exception
               */
              public static InputStream StringTOInputStream(String in) throws Exception{
                  
                  ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
                  return is;
              }
              
              /**
               * 將InputStream轉(zhuǎn)換成byte數(shù)組
               * @param in InputStream
               * @return byte[]
               * @throws IOException
               */
              public static byte[] InputStreamTOByte(InputStream in) throws IOException{
                  
                  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 outStream.toByteArray();
              }
              
              /**
               * 將byte數(shù)組轉(zhuǎn)換成InputStream
               * @param in
               * @return
               * @throws Exception
               */
              public static InputStream byteTOInputStream(byte[] in) throws Exception{
                  
                  ByteArrayInputStream is = new ByteArrayInputStream(in);
                  return is;
              }
              
              /**
               * 將byte數(shù)組轉(zhuǎn)換成String
               * @param in
               * @return
               * @throws Exception
               */
              public static String byteTOString(byte[] in) throws Exception{
                  
                  InputStream is = byteTOInputStream(in);
                  return InputStreamTOString(is);
              }

          }

          posted on 2012-01-09 20:02 AthrunWang 閱讀(1456) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 安国市| 盘山县| 井陉县| 新丰县| 沛县| 宕昌县| 白玉县| 临澧县| 张北县| 封开县| 华亭县| 宣武区| 武夷山市| 叙永县| 呼玛县| 玉林市| 布拖县| 札达县| 称多县| 乌鲁木齐市| 新蔡县| 盱眙县| 蓬莱市| 隆尧县| 阿克苏市| 汽车| 沂水县| 崇义县| 镇沅| 泸西县| 阳信县| 新和县| 罗田县| 鄂托克旗| 丽江市| 三明市| 晴隆县| 报价| 平度市| 南宁市| 新兴县|