隨筆-23  評(píng)論-0  文章-5  trackbacks-0
          String   str   =   "";//add   your   string   content

          InputStream   inputStream   =   new   ByteArrayInputStream(str.getBytes());


           1 package org.kodejava.example.io;
          2
          3  import java.io.ByteArrayInputStream;
          4  import java.io.InputStream;
          5
          6  public class StringToStream {
          7 public static void main(String[] args) {
          8 String text = "Converting String to InputStream Example";
          9
          10 /*
          11 * Convert String to InputString using ByteArrayInputStream class.
          12 * This class constructor takes the string byte array which can be
          13 * done by calling the getBytes() method.
          14 */
          15 try {
          16 InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
          17 } catch (UnsupportedEncodingException e) {
          18 e.printStackTrace();
          19 }
          20 }
          21 }
          22  

           





          1、字符串轉(zhuǎn)inputStream

           

          Java代碼  
          1. String string;  
          2. //......  
          3. InputStream is = new ByteArrayInputStream(string.getBytes());  
           

          2、InputStream轉(zhuǎn)字符串

           

          Java代碼  
          1. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
          2. int i;  
          3. while ((i = is.read()) != -1) {  
          4.     baos.write(i);  
          5. }  
          6. String str = baos.toString();  
          7. System.out.println(str);  
           

          3、String寫入OutputStream

           

          Java代碼  
          1. OutputStream os = System.out;  
          2. os.write(string.getBytes());  
           

          4、OutputStream寫入String

           

          這聽起來有點(diǎn)荒謬,OutputStream本來就是輸出源,還寫入String?

          不過最近項(xiàng)目里確實(shí)遇到了個(gè)類似的問題,比如 SOAPMessage.writeTo(OutputStream os) 這個(gè)方法,是將SOAPMessage的內(nèi)容寫到一個(gè)輸出流中,而我想得到這個(gè)流的內(nèi)容,總不能把他先寫進(jìn)文件再去讀這個(gè)文件吧,研究了好半天,終于想起可以如下這般:

           

          Java代碼  
          1. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
          2. //向OutPutStream中寫入,如 message.writeTo(baos);  
          3. String str = baos.toString();  


          將InputStream/OutputStream轉(zhuǎn)換成string

           

          這里需要用到一個(gè)特殊的類ByteArrayOutputStream,利用他,我們可以將輸出流在內(nèi)存中直接轉(zhuǎn)換成String類型。

          具體代碼如下:

           

          首先從輸入流中將數(shù)據(jù)讀出來寫入ByteArrayOutputStream,然后再將其轉(zhuǎn)換成String.

           

          Java代碼  
          1. InputStream in = urlconn.getInputStream();//獲取輸入流  
          2.   
          3. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
          4.   
          5. //讀取緩存  
          6. byte[] buffer = new byte[2048];  
          7. int length = 0;  
          8. while((length = in.read(buffer)) != -1) {  
          9.     bos.write(buffer, 0, length);//寫入輸出流  
          10. }  
          11. in.close();//讀取完畢,關(guān)閉輸入流  
          12.   
          13. // 根據(jù)輸出流創(chuàng)建字符串對(duì)象  
          14. new String(bos.toByteArray(), "UTF-8");  
          15. //or  
          16. //bos.toString("UTF-8");  

           

          根據(jù)同樣的原理,我們可以將outputstream直接轉(zhuǎn)換成String對(duì)象。



          指定一下字符集
          byte[] b = str.getBytes("utf-8");
          String s = new String(b,"utf-8");



          OUTPUTSTREAM中方法WRITE用法

           void write(byte[] b) 
                    將 b.length 個(gè)字節(jié)從指定的 byte 數(shù)組寫入此輸出流。 
           void write(byte[] b, int off, int len) 
                    將指定 byte 數(shù)組中從偏移量 off 開始的 len 個(gè)字節(jié)寫入此輸出流。 
          abstract  void write(int b) 
                    將指定的字節(jié)寫入此輸出流。 
          轉(zhuǎn)載地址:http://blog.csdn.net/soundtravel/article/details/6927006
          posted on 2013-07-11 16:18 ForMeBlog 閱讀(355) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA基礎(chǔ)類

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 霍山县| 乌拉特后旗| 肇州县| 江门市| 兰溪市| 赞皇县| 涟源市| 行唐县| 安化县| 哈巴河县| 若羌县| 盖州市| 金川县| 棋牌| 繁峙县| 五原县| 于都县| 台中市| 墨江| 新竹县| 宣威市| 麻阳| 志丹县| 清原| 札达县| 万山特区| 易门县| 沙洋县| 缙云县| 朝阳区| 宜昌市| 牡丹江市| 北京市| 安福县| 富民县| 呼和浩特市| 乌什县| 怀远县| 阆中市| 宁津县| 禄丰县|