国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java - 字節流轉成字符串之后,在通過字符串轉成字節流后的文件為什么會不一樣?

瀏覽:114日期:2024-01-05 11:01:54

問題描述

public static void main(String[] args) throws Exception {File sourceFile = new File('/home/joy/圖片/img1-lg.jpg');File tempFile = new File('/home/joy/桌面/TempFile');saveTempFile(sourceFile, tempFile);String str = byteToString(tempFile);File saveFile = new File('/home/joy/桌面/saveFile.jpg');StringTobyte(str,saveFile); } // 保存臨時文件 public static void saveTempFile(File sourceFile, File tempFile) throws Exception {FileInputStream inputStream = new FileInputStream(sourceFile);FileOutputStream fileOutputStream = new FileOutputStream(tempFile);byte[] buf = new byte[10 * 1024];int readLeng = 0;while ((readLeng = inputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, readLeng); fileOutputStream.flush();}fileOutputStream.close();inputStream.close(); } // 文件字節轉字符串 public static String byteToString(File file) throws Exception {StringBuilder returnDatas = new StringBuilder();FileInputStream fileInputStream = new FileInputStream(file);byte[] buf = new byte[10 * 1024];int readLenth = 0;while ((readLenth = fileInputStream.read(buf)) != -1) { returnDatas.append(new String(buf,0,readLenth));}fileInputStream.close();return returnDatas.toString(); } // 字符串轉文件 public static void StringTobyte(String str,File file) throws Exception {FileOutputStream fileOutputStream = new FileOutputStream(file);fileOutputStream.write(str.getBytes());fileOutputStream.close(); }

代碼大意:把一張圖片通過字節流轉成一個字符串,然后在獲取該字符串的字節后還原為文件。理解中,字符只是多帶一層編碼,還原成字節后應該還是能獲取到源文件的呀。

問題解答

回答1:

while ((readLenth = fileInputStream.read(buf)) != -1) { returnDatas.append(new String(buf,0,readLenth));}

二進制圖片文件read出來的不是合法的字符串,其中無法解析的二進制數據會被濾掉參考 javadoc 中的描述:

This method always replaces malformed-input and unmappable-character sequences with this charset’s default replacement string.

回答2:

編碼和解碼不一定是無損的。你從一個圖片文件讀出一個能無損編解碼的字節流的可能性非常小。

new String( new byte[] { 0xff } ).getBytes()=> [-17, -65, -67]回答3:

首先保證序列化,你可以用base64,才能保證轉回來沒問題

標簽: java
主站蜘蛛池模板: 久久com | 成年人黄国产 | 九九亚洲视频 | 香港三级日本三级妇人三级 | 成人a毛片高清视频 | 色老汉丁香网 | 精品国产自在现线看久久 | 精品无码久久久久久国产 | 精品国产一区二区三区不卡 | 国产亚洲高清不卡在线观看 | 亚洲欧美视频一区二区三区 | 国产黄色免费网站 | 国产精品久久久久久久 | 日本天堂视频在线观看 | 97久久国语露脸精品对白 | 欧美一级久久久久久久久大 | 九久久 | 中国一级特黄视频 | 全国最大色成免费网站 | 男女扒开双腿猛进入免费网站 | 在线观看国产情趣免费视频 | 污全彩肉肉无遮挡彩色 | 久草视频首页 | 欧美一级免费大片 | 欧美片a| 国产一级做性视频 | 国产高清视频免费最新在线 | 亚洲美女免费视频 | 国产精品久久久久久网站 | 一色屋成人免费精品网 | 国内国外精品一区二区 | 日韩综合久久 | 一级成人毛片免费观看 | 国产成人免费永久播放视频平台 | 特级淫片欧美高清视频蜜桃 | 日本高清视频www夜色资源 | 久久se精品一区精品二区 | 伊人网五月天 | 久久综合成人网 | 日韩一区二区三区视频在线观看 | 久久精品视屏 |