【发布时间】:2013-11-23 13:08:46
【问题描述】:
好的,所以我一直在尝试使用 FileInputReader.read() 将(“任何”)二进制文件读取到 byte[] 数组...但是无论文件长度如何,它一次只能读取 5 个字节... (顺便说一句,我使用 udp 发送数组/文件)
byte[] array_bytes = new byte[1024];
while((nread=file.read(array_bytes))!=-1){
number_bytesread += array_bytes.length;
socket_udp.send(send_package);
count += 1;
}
-send_package是使用array_bytes发送消息的数据报包
我尝试使用函数read(byte[], offset, lenght),但是如果我把lenght超过5,就会出现这个错误,第一次读取,甚至认为文件肯定大于5 个字节:
nread=file.read(array_bytes, 0, 1024);
不需要偏移,因为我在阅读后立即发送。
Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:272)
at udp_server2.UDP_Server2.Send_Udp(UDP_Server2.java:122)
at udp_server2.UDP_Server2.main(UDP_Server2.java:77)
Java Result: 1
提前感谢您的帮助, 安德烈
【问题讨论】:
标签: java byte java-io indexoutofboundsexception fileinputstream