java implements the method of parsing binary files of string image


1. Requirements description, implementation details:

Parsing 2 files \ case10 \ binary hexadecimal file, which contains a string and 1 image, the data file format for the length of the string data (2 bytes) + + image data string content length (4 bytes) + image data, the data length are data bytes, in the high string for UTF - 8 encoding, please parsing, output string content, image file is saved as a files \ case10 \ test png.

2. Implementation code:

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 years 2 month 7 day   In the morning 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  parsing 2 A binary file

* @param sourceFileName

*

* @author wjggwm

* @data 2017 years 2 month 7 day   In the morning 11:47:12

*/

public static void readFile(String sourceFileName) {

InputStream in = null;

try {

in = new FileInputStream(sourceFileName);



//  Reads the length bytes of string data

byte[] txtLenByte = new byte[2];

in.read(txtLenByte);

int txtlen = byte2ToUnsignedShort(txtLenByte, 0);



//  Read string byte

byte[] txtByte = new byte[txtlen];

in.read(txtByte);

// The string for UTF-8 coding

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

//  Output string

System.out.println(txt);



//  Read the length of image data

byte[] imgLenByte = new byte[4];

in.read(imgLenByte);

int imgLen = byte4ToInt(imgLenByte, 0);



//  Read image data

byte[] img = new byte[imgLen];

in.read(img);

//  Generate image files

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  Writes the bytes to the file

* @param imgName

* @param imgByte

*

* @author wjggwm

* @data 2017 years 2 month 7 day   In the morning 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 Array converted to unsigned short The integer

* @param bytes

* @param off

* @return

*

* @author wjggwm

* @data 2017 years 2 month 7 day   In the morning 11:05:58

*/

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

//  Note that the higher order is at the back, the big and small end

int low = bytes[off];

int high = bytes[off + 1];

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

}



/**

*

* @Description byte Array to int The integer

* @param bytes

* @param off

* @return

*

* @author wjggwm

* @data 2017 years 2 month 7 day   In the morning 11:07:23

*/

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

//  Note that the higher order is at the back, the big and small end

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;

}

}