我想编写一种将文件中的一部分读入字节数组的方法。为此,我正在使用文件输入流和缓冲输入流。
像这样:
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 调试器没有帮助,因为我找不到索引的问题.....