read()和read(byte[])差别在哪里
逐个字节去复制一个文件,效率超慢,都说用read(byte[]),查看api里面的似乎read(byte[])也是循环调用read函数代码如下程序代码:
public int read(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
int c = read();
if (c == -1) {
return -1;
}
b[off] = (byte)c;
int i = 1;
try {
for (; i < len ; i++) {
c = read();
if (c == -1) {
break;
}
b[off + i] = (byte)c;
}
} catch (IOException ee) {
}
return i;
}
不大清楚为什么read(byte[])也是逐个字节从磁盘读?????
----------------解决方案--------------------------------------------------------
read(byte[])会将读来的字节在参数数组中保存的.而read()是不会的.
----------------解决方案--------------------------------------------------------
查看java文档
----------------解决方案--------------------------------------------------------