DMo*_*DMo 0 java fileinputstream
任何人都可以弄清楚为什么我在这里获得索引超出范围的异常?我可以在第一批数据中读取,但是当我尝试再次循环时,它会出错.我也在is.read(readBytes,offset,length)上得到'赋值永不使用'错误; 这也是其中的一部分.
InetAddress address = packet.getAddress();
int port = packet.getPort();
RRQ RRQ = new RRQ();
int offset = 0;
int length = 512;
int block = 0;
byte[] readBytes = new byte[512];
File file = new File(filename);
FileInputStream is = new FileInputStream(file);
int fileBytes = is.read(readBytes, offset, length);
DatagramPacket outPacket = RRQ.doRRQ(readBytes, block, address, port);
socket.send(outPacket);
block++;
if (fileBytes != -1)
{
socket.receive(packet);
offset = offset + fileBytes;
System.out.println(offset);
Exceptions here:fileBytes = is.read(readBytes, offset, length);
outPacket = RRQ.doRRQ(readBytes, block, address, port);
socket.send(outPacket);
block++;
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 10
看看这个电话:
fileBytes = is.read(readBytes, offset, length);
Run Code Online (Sandbox Code Playgroud)
当offset0为0 时这很好- 但是当它不为 0时,你要求将512字节读入一个只有512字节长的数组,但不是从数组的开头开始 - 因此没有足够的空间容纳所有512字节你已经要求了.
我怀疑你想要:
fileBytes = is.read(readBytes, offset, length - offset);
Run Code Online (Sandbox Code Playgroud)
...或者,只是将偏移量保留为0.目前尚不清楚是什么RRQ.doRRQ,但是你怀疑自己没有传入offset......
| 归档时间: |
|
| 查看次数: |
2817 次 |
| 最近记录: |