国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

InputStream,String,File相互轉化

2019-11-10 23:41:44
字體:
來源:轉載
供稿:網友
1. String --> InputStreamInputStream String2InputStream(String str){ ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); return stream;}2. InputStream --> StringString inputStream2String(InputStream is){ BufferedReader in = new BufferedReader(new InputStreamReader(is)); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null){ buffer.append(line); } return buffer.toString();}//今天從網上看到了另一種方法,特拿來分享String all_content=null; try { all_content =new String(); InputStream ins = 獲取的輸入流; ByteArrayOutputStream outputstream = new ByteArrayOutputStream(); byte[] str_b = new byte[1024]; int i = -1; while ((i=ins.read(str_b)) > 0) { outputstream.write(str_b,0,i); } all_content = outputstream.toString(); } catch (Exception e) {e.PRintStackTrace(); }//此兩種方法上面一種更快,但是比較耗內存,后者速度慢,耗資源少3、File --> InputStreamInputStream in = new InputStream(new FileInputStream(File));4、InputStream --> Filepublic void inputstreamtofile(InputStream ins,File file){OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();}

原文地址這里寫鏈接內容


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海林市| 泸西县| 伊宁市| 淳安县| 阳东县| 宜州市| 汤阴县| 老河口市| 民县| 建平县| 嵊州市| 聂拉木县| 长葛市| 白银市| 临湘市| 耒阳市| 崇左市| 鄱阳县| 禹城市| 宁南县| 遵义市| 河南省| 浙江省| 四平市| 娱乐| 文昌市| 桦甸市| 寿光市| 故城县| 桂东县| 赤水市| 精河县| 西藏| 宁城县| 怀宁县| 永宁县| 涟源市| 阜康市| 东光县| 自贡市| 罗山县|