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

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

InputStream,String,File相互轉化

2019-11-10 20:40:20
字體:
來源:轉載
供稿:網友
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();}

原文地址這里寫鏈接內容


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 松阳县| 淮南市| 兴义市| 苏尼特左旗| 瓦房店市| 秦皇岛市| 锦屏县| 博乐市| 河池市| 五指山市| 泸定县| 曲靖市| 巧家县| 景宁| 保定市| 玉屏| 青海省| 盐城市| 三都| 岚皋县| 池州市| 大田县| 深州市| 元氏县| 中西区| 桃江县| 英德市| 密山市| 杭锦旗| 麻阳| 昆山市| 兴国县| 车致| 项城市| 利川市| 德钦县| 马公市| 江达县| 原阳县| 武清区| 正安县|