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

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

InputStream,String,File相互轉(zhuǎn)化

2019-11-10 21:15:56
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
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();}//今天從網(wǎng)上看到了另一種方法,特拿來(lái)分享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(); }//此兩種方法上面一種更快,但是比較耗內(nèi)存,后者速度慢,耗資源少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();}

原文地址這里寫鏈接內(nèi)容


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 股票| 东丰县| 芜湖市| 定西市| 宁河县| 老河口市| 宁都县| 台北县| 方正县| 通化县| 兰西县| 垫江县| 安龙县| 南涧| 赤水市| 衡阳市| 额尔古纳市| 博爱县| 涟水县| 洪雅县| 汤原县| 信阳市| 惠来县| 伊宁市| 二手房| 娄烦县| 缙云县| 静海县| 孟村| 库伦旗| 珲春市| 浦县| 清流县| 乌拉特前旗| 莱西市| 富裕县| 饶平县| 兴海县| 工布江达县| 崇文区| 饶河县|