日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

java 解析二进制_java实现解析二进制文件(字符串、图片)

發(fā)布時(shí)間:2024/9/19 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 解析二进制_java实现解析二进制文件(字符串、图片) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、需求說明,實(shí)現(xiàn)細(xì)節(jié)要求:

解析二進(jìn)制文件 files\case10\binary,其中包含一個(gè)字符串和一張圖片,數(shù)據(jù)文件格式為字符串?dāng)?shù)據(jù)長(zhǎng)度(2字節(jié))+字符串內(nèi)容+圖片數(shù)據(jù)長(zhǎng)度(4字節(jié))+圖片數(shù)據(jù),數(shù)據(jù)長(zhǎng)度均為數(shù)據(jù)字節(jié)長(zhǎng)度,高位在后,字符串為UTF-8編碼,請(qǐng)解析,輸出字符串內(nèi)容,圖片文件保存為files\case10\test.png。

2、實(shí)現(xiàn)代碼:

/**

*

*/

package com.igen.case10;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URISyntaxException;

/**

*

* @ClassName Case10

* @Description TODO

*

* @author wjggwm

* @data 2017年2月7日 上午11:46:25

*/

public class Case10 {

static final String fileName = "/test.png";

static final String filePath = "D:/files/case10";

static final String sourceFileName = "binary";

public static void main(String[] args) {

try {

readFile(Case10.class.getResource(sourceFileName).toURI().getPath());

} catch (URISyntaxException e) {

e.printStackTrace();

}

}

/**

*

* @Description 解析二進(jìn)制文件

* @param sourceFileName

*

* @author wjggwm

* @data 2017年2月7日 上午11:47:12

*/

public static void readFile(String sourceFileName) {

InputStream in = null;

try {

in = new FileInputStream(sourceFileName);

// 讀取字符串?dāng)?shù)據(jù)長(zhǎng)度字節(jié)

byte[] txtLenByte = new byte[2];

in.read(txtLenByte);

int txtlen = byte2ToUnsignedShort(txtLenByte, 0);

// 讀取字符串字節(jié)

byte[] txtByte = new byte[txtlen];

in.read(txtByte);

//字符串為UTF-8編碼

String txt = new String(txtByte, "UTF-8");

// 輸出字符串

System.out.println(txt);

// 讀取圖片數(shù)據(jù)長(zhǎng)度

byte[] imgLenByte = new byte[4];

in.read(imgLenByte);

int imgLen = byte4ToInt(imgLenByte, 0);

// 讀取圖片數(shù)據(jù)

byte[] img = new byte[imgLen];

in.read(img);

// 生成圖片文件

saveToImgByBytes(filePath, fileName, img);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

if (in != null) {

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

*

* @Description 將字節(jié)寫入文件

* @param imgName

* @param imgByte

*

* @author wjggwm

* @data 2017年2月7日 上午11:07:45

*/

public static void saveToImgByBytes(String filePath, String imgName, byte[] imgByte) {

try {

File dic = new File(filePath);

if (!dic.exists()) {

dic.mkdirs();

}

File image = new File(filePath + imgName);

if (!image.exists()) {

image.createNewFile();

}

FileOutputStream fos = new FileOutputStream(image);

fos.write(imgByte);

fos.flush();

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

*

* @Description byte數(shù)組轉(zhuǎn)換為無符號(hào)short整數(shù)

* @param bytes

* @param off

* @return

*

* @author wjggwm

* @data 2017年2月7日 上午11:05:58

*/

public static int byte2ToUnsignedShort(byte[] bytes, int off) {

// 注意高位在后面,即大小端問題

int low = bytes[off];

int high = bytes[off + 1];

return (high << 8 & 0xFF00) | (low & 0xFF);

}

/**

*

* @Description byte數(shù)組轉(zhuǎn)換為int整數(shù)

* @param bytes

* @param off

* @return

*

* @author wjggwm

* @data 2017年2月7日 上午11:07:23

*/

public static int byte4ToInt(byte[] bytes, int off) {

// 注意高位在后面,即大小端問題

int b3 = bytes[off] & 0xFF;

int b2 = bytes[off + 1] & 0xFF;

int b1 = bytes[off + 2] & 0xFF;

int b0 = bytes[off + 3] & 0xFF;

return (b0 << 24) | (b1 << 16) | (b2 << 8) | b3;

}

}

總結(jié)

以上是生活随笔為你收集整理的java 解析二进制_java实现解析二进制文件(字符串、图片)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。