使用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();
}
}
}
}
文件與二進(jìn)制數(shù)據(jù)互轉(zhuǎn)-ByteArrayOutputStream
// 獲取二進(jìn)制數(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)換成二進(jì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;
}
// 二進(jìn)制數(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)文章
mybatis的映射xml中動態(tài)設(shè)置orderby方式
這篇文章主要介紹了mybatis的映射xml中動態(tài)設(shè)置orderby方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Springboot Maven打包跳過測試的五種方式小結(jié)
本文主要介紹了Springboot Maven打包跳過測試的五種方式小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
解決SpringBoot2.1.0+RocketMQ版本沖突問題
這篇文章主要介紹了解決SpringBoot2.1.0+RocketMQ版本沖突問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06
在springboot中如何集成clickhouse進(jìn)行讀寫操作
本文介紹了在Spring Boot中集成ClickHouse的步驟,包括引入依賴、配置數(shù)據(jù)源、編寫實體類和Mapper類進(jìn)行CRUD操作,特別提到批量插入時需要在SQL語句中添加`FORMAT`以避免錯誤,在實際應(yīng)用中,與MySQL的操作類似,只需將ClickHouse當(dāng)作MySQL使用2024-11-11
在Java中實現(xiàn)讓線程按照自己指定的順序執(zhí)行
這篇文章主要介紹了在Java中實現(xiàn)讓線程按照自己指定的順序執(zhí)行,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決
這篇文章主要介紹了使用JDBC連接Mysql 8.0.11出現(xiàn)了各種錯誤的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

