當前位置:
首頁 >
String byte[] stream File之间的相互转换
發布時間:2025/5/22
31
豆豆
生活随笔
收集整理的這篇文章主要介紹了
String byte[] stream File之间的相互转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
//String?轉?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());}}//通過已經知道的文件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());}}//通過給定文件的路徑名構造File對象,如果指定的路徑不存在則先新建對應的文件夾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)?{}}轉載于:https://my.oschina.net/u/195282/blog/224310
總結
以上是生活随笔為你收集整理的String byte[] stream File之间的相互转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CocosEditor 1.0Final
- 下一篇: Android JNI开发摘录(五)之对