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

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

InputStream,String,File相互轉化

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

原文地址這里寫鏈接內容


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乡宁县| 珠海市| 平定县| 封开县| 邹城市| 永昌县| 循化| 漯河市| 邵阳市| 盖州市| 安龙县| 沙河市| 定南县| 莒南县| 中江县| 桂林市| 高阳县| 岳池县| 商都县| 鱼台县| 陇西县| 芜湖县| 宁德市| 芒康县| 太和县| 呈贡县| 突泉县| 卓资县| 正定县| 泸州市| 九龙县| 永靖县| 株洲市| 华容县| 潜山县| 平利县| 巩留县| 安庆市| 田阳县| 罗山县| 武定县|