使用ByteArrayOutputStream寫入字符串方式
更新時間:2021年12月10日 11:03:28 作者:GuoKe0o0
這篇文章主要介紹了使用ByteArrayOutputStream寫入字符串方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
使用ByteArrayOutputStream寫入字符串
package com.gk; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * 使用ByteArrayOutputStream寫入字符串 * @author GuoKe *說明:1,不關(guān)聯(lián)源 2.可以不釋放資源 3.使用toByteArray()獲取數(shù)據(jù) */ public class IOTest8 { public static void main(String[] args) { byte[] dest = null; ByteArrayOutputStream bs = null; try { bs = new ByteArrayOutputStream(); String str = "hello"; byte[] datas = str.getBytes(); bs.write(datas,0,datas.length); bs.flush(); dest = bs.toByteArray(); System.out.println(dest.length + ":" + new String(dest,0,dest.length/*bs.size()*/)); }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }finally { try { if (bs != null) {//alt+shift+z bs.close(); } } catch (Exception e) { e.printStackTrace(); } } } }
文件與二進制數(shù)據(jù)互轉(zhuǎn)-ByteArrayOutputStream
// 獲取二進制數(shù)據(jù) public static byte[] getFileBinary(String filePath) { FileInputStream fis = null; BufferedInputStream bis = null; ByteArrayOutputStream baos = null; try { fis = new FileInputStream(filePath); bis = new BufferedInputStream(fis); baos = new ByteArrayOutputStream(); int c = bis.read(); while (c != -1) { // 數(shù)據(jù)存儲到ByteArrayOutputStream中 baos.write(c); c = bis.read(); } fis.close(); bis.close(); // 轉(zhuǎn)換成二進制 return baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { // 沒有關(guān)閉ByteArrayOutputStream流的意義,空實現(xiàn) try { if (fis != null ) { fis.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis != null ) { bis.close(); } } catch (IOException e) { e.printStackTrace(); } } } return null; } // 二進制數(shù)據(jù)轉(zhuǎn)成文件 public static void binaryToFile(byte[] bytes, String filePath) { FileOutputStream fos = null; BufferedOutputStream bos = null; try { fos = new FileOutputStream(filePath); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fos != null ) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bos != null ) { bos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
ByteArrayOutputStream沒有執(zhí)行close()的意義,原因:底層空實現(xiàn)(源碼如下)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Elasticsearch8.1中的Script使用實例深入解讀
這篇文章主要為大家介紹了Elasticsearch8.1中的Script使用實例深入解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10JUC循環(huán)屏障CyclicBarrier與CountDownLatch區(qū)別詳解
這篇文章主要為大家介紹了JUC循環(huán)屏障CyclicBarrier與CountDownLatch區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12Java 實戰(zhàn)項目錘煉之在線美食網(wǎng)站系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個在線美食網(wǎng)站系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11如何利用Spring?Boot?監(jiān)控?SQL?運行情況
這篇文章主要介紹了如何利用Spring?Boot監(jiān)控SQL運行情況,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07Spring Boot整合JPA使用多個數(shù)據(jù)源的方法步驟
這篇文章主要給大家介紹了關(guān)于Spring Boot整合JPA使用多個數(shù)據(jù)源的方法步驟,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-08-08