byte[] cc2=new byte[10];
int c=br.read(cc2);
System.out.println(new String(cc2,0,c));
这里 c需要-1再放进去吗,
我以为是 假如读取内容长度 为5 如果不减1 就是 0 - 5 那就是多转换了 一个字节
是这样吗?
不需要,因为后边0-c是自动截取到c-1的,也就是0-5,表示0,1,2,3,4,不包括5
看下源码不就知道了
public String(char value[], int offset, int count) {
if (offset < 0) {
throw new StringIndexOutOfBoundsException(offset);
}
if (count <= 0) {
if (count < 0) {
throw new StringIndexOutOfBoundsException(count);
}
if (offset <= value.length) {
this.value = "".value;
return;
}
}
// Note: offset or count might be near -1>>>1.
if (offset > value.length - count) {
throw new StringIndexOutOfBoundsException(offset + count);
}
this.value = Arrays.copyOfRange(value, offset, offset+count);
}