java 文件和byte互轉(zhuǎn)的實例
更新時間:2017年11月16日 09:15:20 作者:BeforeYou
下面小編就為大家分享一篇java 文件和byte互轉(zhuǎn)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
實例如下所示:
/** * 獲得指定文件的byte數(shù)組 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } /** * 根據(jù)byte數(shù)組,生成文件 */ public static void getFile(byte[] bfile, String filePath,String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if(!dir.exists()&&dir.isDirectory()){//判斷文件目錄是否存在 dir.mkdirs(); } file = new File(filePath+"\\"+fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } }
以上這篇java 文件和byte互轉(zhuǎn)的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用正則表達式提取XML節(jié)點內(nèi)容的方法示例
這篇文章主要介紹了Java使用正則表達式提取XML節(jié)點內(nèi)容的方法,結(jié)合具體實例形式分析了java針對xml格式字符串的正則匹配相關(guān)操作技巧,需要的朋友可以參考下2017-08-08SSH框架網(wǎng)上商城項目第18戰(zhàn)之過濾器實現(xiàn)購物登錄功能的判斷
這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第18戰(zhàn):過濾器實現(xiàn)購物登錄功能的判斷,感興趣的小伙伴們可以參考一下2016-06-06使用Stargate訪問K8ssandra的過程之Springboot整合Cassandra
這篇文章主要介紹了使用Stargate訪問K8ssandra的過程之Springboot整合Cassandra,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10IntelliJ IDEA2023中運行Spring Boot找不到VM options進
這篇文章主要介紹了IntelliJ IDEA2023中運行Spring Boot找不到VM options進行端口的修改的問題解決,本文通過圖文并茂的形式給大家介紹的非常詳細,需要的朋友可以參考下2023-11-11