Java中缓冲流FileOutPutStream,BufferWriter
    https://blog.csdn.net/asdfwxy/article/details/106978242

    package com.hy.utils;

    import org.apache.commons.lang.StringUtils;

    import java.io.*;

    /
    @author: qzf
    @date: 2022-04-19 08:45
    @describe: byte 相关操作
    /
    public class ByteUtil {
    /

    合并两个字节数组到一个字节数组

    @param data1 字节数组1
    @param data2 字节数组2
    @return byte[] 合并后的字节数字
    /
    public static byte[] mergeBytes(byte[] data1, byte[] data2) {
    byte[] result = new byte[data1.length + data2.length];
    System.arraycopy(data1, 0, result, 0, data1.length);
    System.arraycopy(data2, 0, result, data1.length, data2.length);
    return result;
    }

    1. /**<br /> * 合并多个字节数组到一个字节数组<br /> *<br /> * @param values 动态字节数字参数<br /> * @return byte[] 合并后的字节数字<br /> */<br /> public static byte[] mergeBytes(byte[]... values) {<br /> int lengthByte = 0;<br /> for (byte[] value : values) {<br /> lengthByte += value.length;<br /> }<br /> byte[] allBytes = new byte[lengthByte];<br /> int countLength = 0;<br /> for (byte[] b : values) {<br /> System.arraycopy(b, 0, allBytes, countLength, b.length);<br /> countLength += b.length;<br /> }<br /> return allBytes;<br /> }
    2. /**<br /> * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。<br /> * 和bytesToInt()配套使用<br /> *<br /> * @param value 要转换的int值<br /> * @return byte数组<br /> */<br /> public static byte[] intToBytesLowAhead(int value) {<br /> byte[] src = new byte[4];<br /> src[3] = (byte) ((value >> 24) & 0xFF);<br /> src[2] = (byte) ((value >> 16) & 0xFF);<br /> src[1] = (byte) ((value >> 8) & 0xFF);<br /> src[0] = (byte) (value & 0xFF);<br /> return src;<br /> }
    3. /**<br /> * 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。<br /> * 和bytesToInt2()配套使用<br /> */<br /> public static byte[] intToBytesHighAhead(int value, int length) {<br /> byte[] src = new byte[length];<br /> if(value == 0){<br /> return src;<br /> }<br /> if(length % 2 != 0){<br /> return src;<br /> }<br /> switch (length){<br /> case 1:<br /> src[0] = (byte) (value & 0xFF);<br /> break;<br /> case 2:<br /> src[0] = (byte) ((value >> 8) & 0xFF);<br /> src[1] = (byte) (value & 0xFF);<br /> break;<br /> case 4:<br /> src[0] = (byte) ((value >> 24) & 0xFF);<br /> src[1] = (byte) ((value >> 16) & 0xFF);<br /> src[2] = (byte) ((value >> 8) & 0xFF);<br /> src[3] = (byte) (value & 0xFF);<br /> break;<br /> case 8:<br /> src[0] = (byte) ((value >> 56) & 0xFF);<br /> src[1] = (byte) ((value >> 48) & 0xFF);<br /> src[2] = (byte) ((value >> 40) & 0xFF);<br /> src[3] = (byte) ((value >> 36) & 0xFF);<br /> src[4] = (byte) ((value >> 24) & 0xFF);<br /> src[5] = (byte) ((value >> 16) & 0xFF);<br /> src[6] = (byte) ((value >> 8) & 0xFF);<br /> src[7] = (byte) (value & 0xFF);<br /> break;<br /> }
    4. return src;<br /> /* byte[] src = new byte[4];<br /> src[0] = (byte) ((value >> 24) & 0xFF);<br /> src[1] = (byte) ((value >> 16) & 0xFF);<br /> src[2] = (byte) ((value >> 8) & 0xFF);<br /> src[3] = (byte) (value & 0xFF);<br /> return src;*/<br /> }
    5. /**<br /> * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序<br /> *<br /> * @param src byte数组<br /> * @param offset 从数组的第offset位开始<br /> * @return int数值<br /> */<br /> public static int bytesToIntLowAhead(byte[] src, int offset) {<br /> return (src[offset] & 0xFF)<br /> | ((src[offset + 1] & 0xFF) << 8)<br /> | ((src[offset + 2] & 0xFF) << 16)<br /> | ((src[offset + 3] & 0xFF) << 24);<br /> }
    6. /**<br /> * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。<br /> */<br /> public static int bytesToIntHighAhead(byte[] bs) {<br /> int bytes = bs.length;<br /> if(bytes == 0){<br /> return 0;<br /> }<br /> if(bytes > 1) {<br /> if((bytes % 2) != 0 || bytes > 4) {<br /> //throw new Exception("not support");<br /> return 0;<br /> }}<br /> switch(bytes) {<br /> case 0:<br /> return 0;<br /> case 1:<br /> return (int)((bs[0] & 0xff));<br /> case 2:<br /> return (int)((bs[0] & 0xff) <<8 | (bs[1] & 0xff));<br /> case 4:<br /> return (int)((bs[0] & 0xffL) <<24 | (bs[1] & 0xffL) << 16 | (bs[2] & 0xffL) <<8 | (bs[3] & 0xffL));<br /> default:<br /> //throw new Exception("not support");<br /> return 0;<br /> }<br /> /* return ((src[offset] & 0xFF) << 24)<br /> | ((src[offset + 1] & 0xFF) << 16)<br /> | ((src[offset + 2] & 0xFF) << 8)<br /> | (src[offset + 3] & 0xFF);*/<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/5/18 14:31<br /> * @describe: byte转无符号整型long(低位在后,高位在前)<br /> * @param: <br /> * @return: <br /> */<br /> public static long bytesToLongHighAhead(byte[] bs) {//throws Exception<br /> int bytes = bs.length;<br /> if(bytes == 0){<br /> return 0;<br /> }<br /> if(bytes > 1) {<br /> if((bytes % 2) != 0 || bytes > 8) {<br /> //throw new Exception("not support");<br /> return 0;<br /> }}<br /> switch(bytes) {<br /> case 0:<br /> return 0;<br /> case 1:<br /> return (long)((bs[0] & 0xff));<br /> case 2:<br /> return (long)((bs[0] & 0xff) <<8 | (bs[1] & 0xff));<br /> case 4:<br /> return (long)((bs[0] & 0xffL) <<24 | (bs[1] & 0xffL) << 16 | (bs[2] & 0xffL) <<8 | (bs[3] & 0xffL));<br /> case 8:<br /> return (long)((bs[0] & 0xffL) <<56 | (bs[1] & 0xffL) << 48 | (bs[2] & 0xffL) <<40 | (bs[3] & 0xffL)<<32 |<br /> (bs[4] & 0xffL) <<24 | (bs[5] & 0xffL) << 16 | (bs[6] & 0xffL) <<8 | (bs[7] & 0xffL));<br /> default:<br /> //throw new Exception("not support");<br /> return 0;<br /> }<br /> //return 0;<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/5/18 14:31<br /> * @describe: long无符号整型转byte(低位在后,高位在前)<br /> * @param:<br /> * @return:<br /> */<br /> public static byte[] longToBytesHighAhead(long value,int length) {<br /> byte[] src = new byte[length];<br /> if(value == 0){<br /> return src;<br /> }<br /> if(length % 2 != 0){<br /> return src;<br /> }<br /> switch (length){<br /> case 1:<br /> src[0] = (byte) (value & 0xFF);<br /> break;<br /> case 2:<br /> src[0] = (byte) ((value >> 8) & 0xFF);<br /> src[1] = (byte) (value & 0xFF);<br /> break;<br /> case 4:<br /> src[0] = (byte) ((value >> 24) & 0xFF);<br /> src[1] = (byte) ((value >> 16) & 0xFF);<br /> src[2] = (byte) ((value >> 8) & 0xFF);<br /> src[3] = (byte) (value & 0xFF);<br /> break;<br /> case 8:<br /> src[0] = (byte) ((value >> 56) & 0xFF);<br /> src[1] = (byte) ((value >> 48) & 0xFF);<br /> src[2] = (byte) ((value >> 40) & 0xFF);<br /> src[3] = (byte) ((value >> 36) & 0xFF);<br /> src[4] = (byte) ((value >> 24) & 0xFF);<br /> src[5] = (byte) ((value >> 16) & 0xFF);<br /> src[6] = (byte) ((value >> 8) & 0xFF);<br /> src[7] = (byte) (value & 0xFF);<br /> break;<br /> }
    7. return src;<br /> }
    8. //bcd码转数字<br /> public static String bcd2Str(byte[] bytes) {<br /> char temp[] = new char[bytes.length * 2], val;
    9. for (int i = 0; i < bytes.length; i++) {<br /> val = (char) (((bytes[i] & 0xf0) >> 4) & 0x0f);<br /> temp[i * 2] = (char) (val > 9 ? val + 'A' - 10 : val + '0');
    10. val = (char) (bytes[i] & 0x0f);<br /> temp[i * 2 + 1] = (char) (val > 9 ? val + 'A' - 10 : val + '0');<br /> }<br /> return new String(temp);<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/14 17:34<br /> * @describe: 字节数组转int(低位在前 高位在后)<br /> * @param:<br /> * @return:<br /> */<br /> public static int bytesToInt(byte[] a){<br /> int rst=0;<br /> for(int i=0;i<4;i++){<br /> rst<<=8;//左移 8 位<br /> rst|= (a[3-i] & 0xff) ;//保存 byte 值到 ans 的最低 8 位上<br /> }<br /> return rst;<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/14 17:36<br /> * @describe: int转字节数组(低位在前 高位在后)<br /> * @param:<br /> * @return:<br /> */<br /> public static byte[] intToBytes(int a){<br /> byte[] rst=new byte[4];<br /> for(int i=0;i<4;i++)<br /> rst[i]=(byte)(a>>(i*8));//截断 int 的低 8 位为一个字节 byte,并存储起来<br /> return rst;<br /> }<br /> /*public static int MyBytesToInt(byte[] source){
    11. byte[] finalarr = new byte[4];<br /> int offset = 4 - source.length;<br /> for(int i = offset;i < 4;i++){<br /> finalarr[i] = source[i - offset];<br /> }<br /> return bytesToIntHighAhead(finalarr);<br /> }*/<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/15 10:02<br /> * @describe: 数组逆序<br /> * @param:<br /> * @return:<br /> */<br /> public static void reverse(byte[] array) {<br /> //利用循环,实现数组遍历,遍历过程中,最远端换位<br /> //for的第一项,定义2个变量, 最后,两个变量++ --<br /> for( int min = 0 , max = array.length-1 ; min < max ; min++,max--){<br /> //对数组中的元素,进行位置交换<br /> //min索引和max索引的元素交换<br /> //定义变量,保存min索引<br /> byte temp = array[min];<br /> //max索引上的元素,赋值给min索引<br /> array[min] = array[max];<br /> //临时变量,保存的数据,赋值到max索引上<br /> array[max] = temp;<br /> }<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/18 14:56<br /> * @describe: byte -> hex<br /> * @param:<br /> * @return:<br /> */<br /> public static String hexToByte(byte b){<br /> return String.format("%02X", b);<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/18 14:57<br /> * @describe: byte -> hex<br /> * @param:<br /> * @return:<br /> */<br /> public static String bytesToHex(byte[] bytes){<br /> if(bytes == null) return "";<br /> String str = "";<br /> for(int i=0; i<bytes.length; i++){<br /> str += String.format("%02X", bytes[i]);<br /> }<br /> return str;<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/5/19 10:04<br /> * @describe:<br /> * @param:<br /> * @return:<br /> */<br /> public static byte[] hexToBytes(String hexString) {<br /> hexString = hexString.replaceAll("\\n", "").replaceAll("\\r", "").trim().replaceAll(" ", "").replaceAll(" ", "");<br /> final byte[] byteArray = new byte[hexString.length() / 2];<br /> int k = 0;<br /> for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先<br /> byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);<br /> byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);<br /> byteArray[i] = (byte) (high << 4 | low);<br /> k += 2;<br /> }<br /> return byteArray;<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/18 15:11<br /> * @describe: bytes -> hex<br /> * @param:<br /> * @return:<br /> */<br /> public static String printHexShort(int s){<br /> byte[] bytes = hexShort(s);<br /> return bytesToHex(bytes);<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/18 15:12<br /> * @describe: short -> bytes<br /> * @param:<br /> * @return:<br /> */<br /> public static byte[] hexShort(int s){<br /> byte[] bytes = new byte[2];<br /> bytes[0] = (byte)((s << 24) >> 24);<br /> bytes[1] = (byte)((s << 16) >> 24);<br /> return bytes;<br /> }<br /> /**<br /> * @author: qzf<br /> * @date: 2022/4/18 15:12<br /> * @describe: int -> bytes<br /> * @param:<br /> * @return:<br /> */<br /> public static byte[] hexInt(int n){<br /> byte[] bytes = new byte[4];<br /> bytes[3] = (byte) ((n ) >> 24);<br /> bytes[2] = (byte) ((n << 8) >> 24);<br /> bytes[1] = (byte) ((n << 16) >> 24);<br /> bytes[0] = (byte) ((n << 24) >> 24);<br /> return bytes;<br /> }<br /> //将文件转换成Byte数组<br /> public static byte[] getBytesByFile(String pathStr) {<br /> File file = new File(pathStr);<br /> try {<br /> FileInputStream fis = new FileInputStream(file);<br /> ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);<br /> byte[] b = new byte[1000];<br /> int n;<br /> while ((n = fis.read(b)) != -1) {<br /> bos.write(b, 0, n);<br /> }<br /> fis.close();<br /> byte[] data = bos.toByteArray();<br /> bos.close();<br /> return data;<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> return null;<br /> }
    12. //将Byte数组转换成文件<br /> public static void getFileByBytes(byte[] bytes, String filePath, String fileName) {<br /> BufferedOutputStream bos = null;<br /> FileOutputStream fos = null;<br /> File file = null;<br /> try {<br /> File dir = new File(filePath);<br /> if (!dir.exists() ) {// 判断文件目录是否存在<br /> dir.mkdirs();<br /> }<br /> file = new File(filePath + "/" + fileName);<br /> fos = new FileOutputStream(file);<br /> bos = new BufferedOutputStream(fos);<br /> bos.write(bytes);<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> } finally {<br /> if (bos != null) {<br /> try {<br /> bos.close();<br /> } catch (IOException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> if (fos != null) {<br /> try {<br /> fos.close();<br /> } catch (IOException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> }<br /> }<br /> //累加和校验 ,所有字节相加后,取最后两个字节校验(高位在前,低位在后)<br /> public static String checkSum(String data) {<br /> if (data == null || data.equals("")) {<br /> return "";<br /> }<br /> int total = 0;<br /> int len = data.length();<br /> int num = 0;<br /> while (num < len) {<br /> String s = data.substring(num, num + 2);<br /> System.out.println(s);<br /> total += Integer.parseInt(s, 16);<br /> num = num + 2;<br /> }<br /> /**<br /> * 用256求余最大是255,即16进制的FF<br /> */<br /> int mod = total % 256;<br /> String hex = Integer.toHexString(mod);<br /> len = hex.length();<br /> // 如果不够校验位的长度,补0,这里用的是两位校验<br /> if (len < 2) {<br /> hex = "0" + hex;<br /> }<br /> return hex;<br /> }<br /> //累加和校验 ,所有字节相加后,取最后两个字节校验(高位在前,低位在后)<br /> public static byte[] checkSum(byte[] arr) {
    13. int total = 0;<br /> int len = arr.length;<br /> for(int i = 0;i < len;i++){<br /> total += (arr[i] & 0xff);<br /> }<br /> byte[] rst = intToBytesHighAhead(total, 2);<br /> return rst;<br /> }<br /> //获取字符串前4位的年份<br /> public static int getYear(String date){<br /> try{<br /> date = StringUtils.trim(date);<br /> if(StringUtil.isEmpty(date)) return 0;<br /> if(date.length()<4) return 0;<br /> return Integer.valueOf(date.substring(0,4));<br /> }catch(Exception e){<br /> e.printStackTrace();<br /> return 0;<br /> }<br /> }<br /> public static void main(String[] args) {<br /> try {<br /> byte[] bytes = longToBytesHighAhead(4294967295L, 2);<br /> long l = bytesToLongHighAhead(new byte[]{-1,-1,-1,-1});<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br />}