日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java string 转 inputstream_String和inputstream互转【转文】

發(fā)布時間:2024/7/23 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java string 转 inputstream_String和inputstream互转【转文】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

URLConnection urlConn = url.openConnection(); // 打開網(wǎng)站鏈接s

BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8")); // 實例化輸入流,并獲取網(wǎng)頁代碼

String s; // 依次循環(huán),至到讀的值為空

StringBuilder sb = new StringBuilder();

while ((s = reader.readLine()) != null) {

sb.append(s);

}

reader.close();

String str = sb.toString();

====================下面的方法有點惡心,改了改,看起來好多了===========================

String ? str ? = ? "";//add ? your ? string ? content

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

1 packageorg.kodejava.example.io;2 3 importjava.io.ByteArrayInputStream;4 importjava.io.InputStream;5 6 publicclassStringToStream {7 publicstaticvoidmain(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 be13 * done by calling the getBytes() method.14 */15 try{16 InputStream is=newByteArrayInputStream(text.getBytes("UTF-8"));17 }catch(UnsupportedEncodingException e) {18 e.printStackTrace();19 }20 }21 }22

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

Java代碼??

String?string;

//......

InputStream?is?=?newByteArrayInputStream(string.getBytes());

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

Java代碼??

ByteArrayOutputStream?baos?=newByteArrayOutputStream();

inti;

while((i?=?is.read())?!=?-1)?{

baos.write(i);

}

String?str?=?baos.toString();

System.out.println(str);

3、String寫入OutputStream

Java代碼??

OutputStream?os?=?System.out;

os.write(string.getBytes());

4、OutputStream寫入String

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

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

Java代碼??

ByteArrayOutputStream?baos?=newByteArrayOutputStream();

//向OutPutStream中寫入,如?message.writeTo(baos);

String?str?=?baos.toString();

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

具體代碼如下:

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

Java代碼??

InputStream?in?=?urlconn.getInputStream();//獲取輸入流

ByteArrayOutputStream?bos?=?newByteArrayOutputStream();

//讀取緩存

byte[]?buffer?=newbyte[2048];

intlength?=0;

while((length?=?in.read(buffer))?!=?-1)?{

bos.write(buffer,?0,?length);//寫入輸出流

}

in.close();//讀取完畢,關閉輸入流

//?根據(jù)輸出流創(chuàng)建字符串對象

newString(bos.toByteArray(),"UTF-8");

//or

//bos.toString("UTF-8");

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

指定一下字符集

byte[] b = str.getBytes("utf-8");

String s = new String(b,"utf-8");

OUTPUTSTREAM中方法WRITE用法

void write(byte[] b)

將 b.length 個字節(jié)從指定的 byte 數(shù)組寫入此輸出流。

void write(byte[] b, int off, int len)

將指定 byte 數(shù)組中從偏移量 off 開始的 len 個字節(jié)寫入此輸出流。

abstract void write(int b)

將指定的字節(jié)寫入此輸出流。

總結(jié)

以上是生活随笔為你收集整理的java string 转 inputstream_String和inputstream互转【转文】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。