String byte[] stream File之间的相互转换
生活随笔
收集整理的這篇文章主要介紹了
String byte[] stream File之间的相互转换
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
//String?轉(zhuǎn)?Stream public?static?InputStream?stringTOInputStream(ByteString?webpage)throws?Exception?{ByteArrayInputStream?is?=?new?ByteArrayInputStream(webpage.toByteArray());return?is;}//Stream?保存為指定的文件public?static?void?inputstreamtofile(InputStream?ins,?File?file)?{try?{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();}?catch?(Exception?e)?{e.printStackTrace();android.util.Log.d("PADATEST","inputstreamtofile?="?+?e.getMessage());}}//通過(guò)已經(jīng)知道的文件URL保存為指定的文件public?void?saveUrlImg(String?url,?File?file)?{ByteArrayOutputStream?os?=?null;byte[]?result?=?null;try?{InputStream?is?=?new?java.net.URL(url).openStream();inputstreamtofile(is,?file);is.close();}?catch?(Exception?e)?{e.printStackTrace();android.util.Log.d("PADATEST",?"saveUrlImg?="?+?e.getMessage());}}//通過(guò)給定文件的路徑名構(gòu)造File對(duì)象,如果指定的路徑不存在則先新建對(duì)應(yīng)的文件夾public?static?File?getFilePath(String?filePath,?String?fileName)?{File?file?=?null;makeRootDirectory(filePath);try?{file?=?new?File(filePath?+?fileName);}?catch?(Exception?e)?{e.printStackTrace();}return?file;}public?static?void?makeRootDirectory(String?filePath)?{android.util.Log.d("PADATEST",?"filePath?="?+?filePath);File?file?=?null;try?{file?=?new?File(filePath);if?(!file.exists())?{file.mkdirs();}}?catch?(Exception?e)?{}}轉(zhuǎn)載于:https://my.oschina.net/u/195282/blog/224310
總結(jié)
以上是生活随笔為你收集整理的String byte[] stream File之间的相互转换的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CocosEditor 1.0Final
- 下一篇: Android JNI开发摘录(五)之对