我想编写一个方法,从文件中读取部分字节数组。 为此,我正在使用fileinputstream和缓冲输入流。Java BufferedInputStream.read()IndexOutOfBounds
这样的:
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
我通过调用一个方法名 “的OpenFile(字符串文件)” 做一次。 一旦文件已被打开与该方法中,我尝试与功能操作:“ReadParts(字节[]缓冲器,诠释抵消,诠释LEN)”
dis.read(buffer, offset, len);
for(int i = 0; i < buffer.length; i++) System.out.print((char)buffer[i]);
// used data:
// file = "C:\Temp\test.txt" with a size of 949
// buffer: always in this case with a size of 237, except for the last one its 238
// offsets: 0, 237, 474, 711
// len is always 237, except for the last one its 238
线dis.read()后抛出第一步总是一个indexOutOfBounds错误消息,但我无法弄清楚为什么和什么。使用netbeans调试器没有帮助,因为我找不到索引的问题.....
请张贴在那里你计算偏移代码和len并在您初始化缓冲区。这很可能会导致你的问题。 – AlexS 2012-03-14 11:51:36