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ù)。