正常不包含中文的话应该是2个byte应该是一个字符吧,你每2位截取一下不就行了,只要你知道DDD的位置,不把Byte转换成String,怎么截取啊byte里应该没什么规律吧,谁提的这么个破需求啊

解决方案 »

  1.   

    楼主参考一下:import java.util.*;public class TrancateByteArray {

    public static void main(String[] args) {
    String start = "FFF";
    String end = "DDD"; byte[] startByteArray = start.getBytes();
    byte[] endByteArray = end.getBytes();

    byte[] originalByteArray = { 
    104,104,104,104,45,
    31,32,33,34,35,36,37,38,
    68, 68, 68, 104, 104, 104, 104, 45,

    70, 70, 70,
    65,66,67,68,45,45,45,31,
    68, 68, 68, 104, 104, 104, 104, 45,

    70, 70, 70,
    65,66,67,68,69,45,45,45,32,
    68, 68, 68, 104, 104, 104, 104, 45, 70, 70, 70,
    65,66,67,68,69,70,45,45,45,33,
    68, 68, 68, 104, 104, 104, 104, 45
    };

    List<byte[]> listResult = getByteArrays(originalByteArray, startByteArray, endByteArray);
    System.out.println(listResult.size());//找到几组
    Iterator<byte[]> it = listResult.iterator();
    while(it.hasNext()) {
    byte[] result = it.next();
    System.out.println("----------------------------------------------------------------");
    System.out.println(Arrays.toString(result));
    }
    System.out.println("the end!");
    } /**
     * 以byte1 为起始, byte2为结束,分离toBeSplit。
     * @param toBeSplit
     * @param byte1
     * @param byte2
     * @return
     */
    public static List<byte[]> getByteArrays (byte[] toBeSplit, byte[] byte1, byte[] byte2) {
    List<byte[]> listByte = new ArrayList<byte[]>();
    if(toBeSplit.length <= byte1.length + byte2.length) {
    System.out.println("数组数据太少!");
    return listByte; //listByte 为空。
    }
    byte[] temp = null;
    int start = 0;
    int end = 0; for(int i = 0; i < toBeSplit.length - (byte1.length + byte2.length) ; i++) {
    //外循环,匹配起始数组
    byte[] tempByteArrayStart = Arrays.copyOfRange(toBeSplit, i, i + byte1.length);
    if(Arrays.equals(tempByteArrayStart, byte1)) {
    start = i;
    //内循环,匹配结束数组
    for(int j = start + byte1.length; j < toBeSplit.length - byte2.length + 1 ; j++ ) {
    byte[] tempByteArrayEnd = Arrays.copyOfRange(toBeSplit, j, j + byte2.length);
    //找到前后匹配,获取中间部分,放一数组里,再添加list里。
    if(Arrays.equals(tempByteArrayEnd, byte2)) {
    end = j;
    temp = Arrays.copyOfRange(toBeSplit, start + byte1.length, end);
    listByte.add(temp);
    i = (end + byte1.length - 1);
    break;
    }
    }
    if(i == start) {//相等,说明找到了最后,也没有找到,程序结束循环; 否则,继续寻找。
    break;
    }
    }
    }
    return listByte;
    }
    }
      

  2.   

    改写一下方法,提高效率。 /**
     * 以byte1 为起始, byte2为结束,分离toBeSplit。
     * @param toBeSplit
     * @param byte1
     * @param byte2
     * @return
     */
    public static List<byte[]> getByteArrays (byte[] toBeSplit, byte[] byte1, byte[] byte2) {
    List<byte[]> listByte = new ArrayList<byte[]>();
    if(toBeSplit.length <= byte1.length + byte2.length) {
    System.out.println("数组数据太少!");
    return listByte; //listByte 为空。
    }
    byte[] temp = null;
    int start = 0;
    int end = 0; for(int i = 0; i < toBeSplit.length - (byte1.length + byte2.length) ; i++) {
    //提高效率
    if(toBeSplit[i] != byte1[0]) {//首个字节匹配否?
    continue;
    }
    //外循环,匹配起始数组
    byte[] tempByteArrayStart = Arrays.copyOfRange(toBeSplit, i, i + byte1.length);
    if(Arrays.equals(tempByteArrayStart, byte1)) {
    start = i;
    //内循环,匹配结束数组
    for(int j = start + byte1.length; j < toBeSplit.length - byte2.length + 1 ; j++ ) {
    //判断首个字节, 来提高效率
    if(toBeSplit[j] != byte2[0]) {
    continue;
    }
    byte[] tempByteArrayEnd = Arrays.copyOfRange(toBeSplit, j, j + byte2.length);
    //找到前后匹配,获取中间部分,放一数组里,再添加list里。
    if(Arrays.equals(tempByteArrayEnd, byte2)) {
    end = j;
    temp = Arrays.copyOfRange(toBeSplit, start + byte1.length, end);
    listByte.add(temp);
    i = (end + byte1.length - 1);
    break;
    }
    }
    if(i == start) {//相等,说明找到了最后,也没有找到,程序结束循环; 否则,继续寻找。
    break;
    }
    }
    }
    return listByte;
    }