当前位置: 移动技术网 > IT编程>开发语言>Java > java实现解析二进制文件的方法(字符串、图片)

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

营养快线广告,鹿死谁手,辫的组词

1、需求说明,实现细节要求:

解析二进制文件 files\case10\binary,其中包含一个字符串和一张图片,数据文件格式为字符串数据长度(2字节)+字符串内容+图片数据长度(4字节)+图片数据,数据长度均为数据字节长度,高位在后,字符串为utf-8编码,请解析,输出字符串内容,图片文件保存为files\case10\test.png。

2、实现代码:

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 解析二进制文件

* @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);

 

// 读取字符串数据长度字节

byte[] txtlenbyte = new byte[2];

in.read(txtlenbyte);

int txtlen = byte2tounsignedshort(txtlenbyte, 0);

 

// 读取字符串字节

byte[] txtbyte = new byte[txtlen];

in.read(txtbyte);

//字符串为utf-8编码

string txt = new string(txtbyte, "utf-8");

// 输出字符串

system.out.println(txt);

 

// 读取图片数据长度

byte[] imglenbyte = new byte[4];

in.read(imglenbyte);

int imglen = byte4toint(imglenbyte, 0);

 

// 读取图片数据

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 将字节写入文件

* @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数组转换为无符号short整数

* @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数组转换为int整数

* @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;

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网