获取及修改文件头的前两个字节--Java实现

package cn.myjava.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.FileInputStream;
import java.io.RandomAccessFile;

/**
 * @author YPF
 */
@Slf4j
public class FileInfoUtil {
    /**
     * 修改文件头的前两个字节
     * @param header
     * @param filePath
     * @throws Exception
     */
    public static void modifyFileHeader(byte[] header, String filePath) {
        if (header.length == 2) {
            try (RandomAccessFile src = new RandomAccessFile(filePath, "rw")) {
                int srcLength = (int)src.length();
                // 略过前两个字节
                src.skipBytes(2);
                byte[] buff = new byte[srcLength - 2];
                // 读取除前两个字节之后的字节
                src.read(buff);
                src.seek(0);
                src.write(header);
                src.seek(header.length);
                src.write(buff);
            } catch (Exception e) {
                log.error("修改文件{}的前两个字节失败!", filePath);
            }
        }
    }

    /**
     * 根据文件路径获取文件头前两个字节
     *
     * @param filePath 文件路径
     * @return 文件头前两个字节信息
     */
    public static String getFileHeader(String filePath) {
        String value = null;
        try (FileInputStream is = new FileInputStream(filePath)) {
            byte[] b = new byte[2];
            is.read(b, 0, b.length);
            value = bytesToHexString(b);
        } catch (Exception e) {
            log.error("获取文件{}的前两个字节失败!", filePath);
        }
        return value;
    }

    /**
     * 将byte数组转换成string类型表示
     * @param src
     * @return
     */
    private static String bytesToHexString(byte[] src) {
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        String hv;
        for (int i = 0; i < src.length; i++) {
            // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
            hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
            if (hv.length() < 2) {
                builder.append(0);
            }
            builder.append(hv);
        }

        return builder.toString();
    }

    /**
     * 将Hex String转换为Byte数组
     *
     * @param hexString the hex string
     * @return the byte [ ]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (StringUtils.isEmpty(hexString)) {
            return null;
        }
        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() >> 1];
        int index = 0;
        for (int i = 0; i < hexString.length(); i++) {
            if (index  > hexString.length() - 1) {
                return byteArray;
            }
            byte highDit = (byte) (Character.digit(hexString.charAt(index), 16) & 0xFF);
            byte lowDit = (byte) (Character.digit(hexString.charAt(index + 1), 16) & 0xFF);
            byteArray[i] = (byte) (highDit << 4 | lowDit);
            index += 2;
        }
        return byteArray;
    }
}

你可能感兴趣的:(获取及修改文件头的前两个字节--Java实现)