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;
} }

  

java实现解析二进制文件(字符串、图片)的更多相关文章

  1. JAVA代码解析String字符串(json格式的)

    java解析String字符串(json格式) 需要jar包:json-lib-2.4-jdk15.jar 一. String str = "{\"name\":\&qu ...

  2. Java解析json字符串和json数组

    Java解析json字符串和json数组 public static Map<String, String> getUploadTransactions(String json){ Map ...

  3. java解析Json字符串之懒人大法

    面对Java解析Json字符串的需求,有很多开源工具供我们选择,如google的Gson.阿里巴巴的fastJson.在网上能找到大量的文章讲解这些工具的使用方法.我也是参考这些文章封装了自己的Jso ...

  4. Java基础-处理json字符串解析案例

    Java基础-处理json字符串解析案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 作为一名开发人员,想必大家或多或少都有接触到XML文件,XML全称为“extensible ...

  5. Java常量池解析与字符串intern简介

    在Java应用程序运行时,Java虚拟机会保存一份内部的运行时常量池,它区别于class文件的常量池,是class文件常量池映射到虚拟机中的数据结构. 关于class文件常量池的部分可以参考之前的博文 ...

  6. java解析xml字符串(用dom4j)

    package com.smsServer.Dhst; import java.util.HashMap; import java.util.Iterator; import java.util.Ma ...

  7. objective-c和java下解析对象类型和数组类型JSON字符串

    首先讲objective-c如何实现: 这里需要用到2个插件,一个是JSONKit,另一个是Jastor,一共包含6个文件,3个.h头文件和3个.m实现文件.在ARC的工程中如何导入不支持ARC的第三 ...

  8. java解析xml字符串为实体(dom4j解析)

    package com.smsServer.Dhst; import java.util.HashMap; import java.util.Iterator; import java.util.Ma ...

  9. java后台处理解析json字符串的两种方式

    简单说一下背景 上次后端通过模拟http请求百度地图接口,得到的是一个json字符串,而我只需要其中的某个key对应的value. 当时我是通过截取字符串取的,后来觉得不太合理,今天整理出了两种处理解 ...

随机推荐

  1. iOS学习基本常识

    转发至:http://blog.sina.com.cn/s/blog_9266da3d010184i0.html 1. 了解main函数,  UIApplication是初始化程序的核心,它接受4个参 ...

  2. margin 相关 bug 系列

    原文地址:margin系列之bug巡演 by @doyoe IE6浮动双倍margin bug 这当是IE6最为经典的bug之一. 触发方式 元素被设置浮动 元素在与浮动一致的方向上设置margin值 ...

  3. quailty's Contest #1 A1 道路修建 Small

    暴力.每次合并两个点之后,把新产生的连通关系都记录下来. #include<cstdio> #include<algorithm> #include<vector> ...

  4. html-div-css

    用CSS实现拉动滚动条时固定网页背景不动   body{        background-image: url(./inc/bgbk.jpg);        background-attachm ...

  5. 关于OC和Swift使用GIT创建项目

    1.先进入码云,点击自己的头像 ->   ,2.里面有一个SSH公钥,点击   ,3.之后在终端输入 ssh-keygen -t rsa -C “xxxxx@xxx.com”,注意:”” 要用英 ...

  6. java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类?

    java中有几种类型的流?JDK为每种类型的流提供了一些抽象类以供继承,请说出他们分别是哪些类? Java中的流分为两种,一种是字节流,另一种是字符流,分别由四个抽象类来表示(每种流包括输入和输出两种 ...

  7. VS2010与SVN

    http://blog.sina.com.cn/s/blog_4fe44775010182yl.html 在VS2010中使用SVN,必须先安装SVN的客户端,再安装VisualSVN(SVN的插件) ...

  8. iOS 添加导航按钮

    iOS设置导航按钮navigationBar中包含了这几个重要组成部分:leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title. ...

  9. LPC1768的USB使用-枚举过程

    枚举过程如下 #ifndef __USBCORE_H__ #define __USBCORE_H__ /* USB端点0 发送数据结构体*/ typedef struct _USB_EP_DATA { ...

  10. 使用DLL进行不同语言之间的调用(转)

    源:使用DLL进行不同语言之间的调用 __declspec(dllexport) 是告诉编译器用来导出函数的,在代码中不另作说明了. extern "C" 的意思就是用C的方式来导 ...