【发布时间】:2012-03-14 11:21:09
【问题描述】:
我想编写一个将文件中的一部分读入字节数组的方法。 为此,我正在使用文件输入流和缓冲输入流。
像这样:
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
我只通过调用方法名称“OpenFile(String File)”来执行此操作一次。 使用此方法打开文件后,我尝试使用以下函数进行操作:“ReadParts(byte[] buffer, int offset, int 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 调试器没有帮助,因为我找不到索引的问题.....
【问题讨论】:
-
请在计算偏移量和长度以及初始化缓冲区的位置发布代码。这很可能会导致您的问题。
标签: java java-io bufferedinputstream datainputstream