Java輸入流和字符串互相轉(zhuǎn)換

Java輸入流和字符串互相轉(zhuǎn)換

深圳達(dá)內(nèi)教育      2022-04-02 20:20:01     3

Java輸入流和字符串互相轉(zhuǎn)換,import?java.io.BufferedReader;import?java.io.ByteArrayInputStream;import?java.io.InputStream;import?java.io.InputStr

課程價(jià)格 請(qǐng)咨詢

上課時(shí)段: 授課校區(qū):

詳細(xì)介紹

import?java.io.BufferedReader;import?java.io.ByteArrayInputStream;import?java.io.InputStream;import?java.io.InputStreamReader;?public?class?Test4?{?????public?static?void?main(String[]?args)?{????????String?str?=?"我在深圳";????????InputStream?is?=?StreamUtil.getStrToStream(str);????????System.out.println(str);????????System.out.println(StreamUtil.getStreamToStr(is));????????????}}?class?StreamUtil?{?????????public?static?InputStream?getStrToStream(String?sInputString)?{????????if?(sInputString?!=?null?&&?!sInputString.trim().equals(""))?{????????????try?{????????????????ByteArrayInputStream?tInputStringStream?=?new?ByteArrayInputStream(sInputString.getBytes());????????????????return?tInputStringStream;????????????}?catch?(Exception?ex)?{????????????????ex.printStackTrace();????????????}????????}????????return?null;????}?????????public?static?String?getStreamToStr(InputStream?tInputStream)?{????????if?(tInputStream?!=?null)?{????????????try?{????????????????BufferedReader?tBufferedReader?=?new?BufferedReader(new?InputStreamReader(tInputStream));????????????????StringBuffer?tStringBuffer?=?new?StringBuffer();????????????????String?sTempOneLine;????????????????while?((sTempOneLine?=?tBufferedReader.readLine())?!=?null)?{????????????????????tStringBuffer.append(sTempOneLine);????????????????}????????????????return?tStringBuffer.toString();????????????}?catch?(Exception?ex)?{????????????????ex.printStackTrace();????????????}????????}????????return?null;????}?}

以上就是深圳達(dá)內(nèi)教育java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“Java輸入流和字符串互相轉(zhuǎn)換”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。

培訓(xùn)啦提醒您:交易時(shí)請(qǐng)核實(shí)對(duì)方資質(zhì),對(duì)于過(guò)大宣傳或承諾需謹(jǐn)慎!任何要求預(yù)付定金、匯款等方式均存在風(fēng)險(xiǎn),謹(jǐn)防上當(dāng)。