我现在有一个byte[] 每次存1600个字节,存一次用StringBuffer.append()加在后面,那么我通过StringBuffer.delete(0,3)的方法,删除的是一个字节数组里面的前4个字节,还是删除的前4个字节数组。

解决方案 »

  1.   

    前四个字节下面是我写的测试类,做为参考,lz可以参考一下public class Test {
    public static void main(String[] args) {                           StringBuffer sbWhere = new StringBuffer();
            char[] c = {'a','b','c','d','e','f','g'};
            sbWhere.append(c);
            sbWhere.delete(0, 3);
            System.out.println(sbWhere.toString());
            }
    }结果:defg
    说明删除的是前四个字节数组
      

  2.   


    我刚也测试了下。其实不是删的字节数组,你把每个字节数组里面多写几个试试,删的时候其实是将字节数组变成了字符串“abcdefg”,然后删除的是abcd这四个字符。其实跟字符数组没有关系
      

  3.   

    LZ好像自己醒悟过来了
    StringBuffer的delete是删除StringBuffer对象的字符串信息,并不会删除原来的数组
      

  4.   

    可以用ByteArrayInputStream和ByteArrayOutputStream
    比如
    byte[] b1 = {1,2,3,4};
    byte[] b2 = {5,6,7,8};ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(b1, 0, b1.length);
    bos.write(b2, 0, b2.length); 
    byte[] b3 = bos.toByteArray(); //相当于b2接到b1后面,得到一个新的byte数组b3ByteArrayInputStream bis = new ByteArrayInputStream(b3); 
    int n = 0;
    while ((n=bis.read()) != -1) { //从字节流中读入字节并输出
        System.out.println(n);
    }