Java BufferedInputStream.read()索引自动边界

Java BufferedInputStream.read()索引自动边界,java,java-io,bufferedinputstream,datainputstream,Java,Java Io,Bufferedinputstream,Datainputstream,我想编写一个方法,将部分文件读入字节数组。 为此,我使用fileinputstream和缓冲inputstream 像这样: fis = new FileInputStream(file); bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); 我只通过调用一个方法名“OpenFile(stringfile)”来完成一次。 使用此方法打开文件后,我尝试使用函数“ReadParts(byte[]buffer,int

我想编写一个方法,将部分文件读入字节数组。 为此,我使用fileinputstream和缓冲inputstream

像这样:

fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
我只通过调用一个方法名“OpenFile(stringfile)”来完成一次。 使用此方法打开文件后,我尝试使用函数“ReadParts(byte[]buffer,int offset,int len)”进行操作

dis.read(缓冲区、偏移量、len);
对于(inti=0;i

在第一步之后,dis.read()行始终会抛出一条indexOutOfBounds errormessage,但我不知道原因和内容。使用netbeans调试器没有任何帮助,因为我找不到索引的问题….

您如何处理offset和len。我现在猜你的偏移量+len大于缓冲区。

你是如何得出偏移量和len的。我猜现在偏移量+len大于缓冲区。

偏移量是缓冲区中的偏移量,而不是文件

我怀疑你想要的是

byte[] buffer = new byte[237];

int len = dis.read(buffer); // read another 237 bytes.

if (len < 0) throw new EOFException(); // no more data.
for(int i = 0; i < len; i++)
   System.out.print((char)buffer[i]);
// or
System.out.print(new String(buffer, 0, 0, len));
byte[]buffer=新字节[237];
int len=dis.read(缓冲区);//再读取237个字节。
如果(len<0)抛出新的EOFEException();//没有更多的数据。
对于(int i=0;i
在调试器中,是否可以检查偏移量>=0和偏移量+长度b.length-off){ 抛出新的IndexOutOfBoundsException();
检查的条件之一无效。

偏移量是缓冲区中的偏移量,而不是文件中的偏移量

我怀疑你想要的是

byte[] buffer = new byte[237];

int len = dis.read(buffer); // read another 237 bytes.

if (len < 0) throw new EOFException(); // no more data.
for(int i = 0; i < len; i++)
   System.out.print((char)buffer[i]);
// or
System.out.print(new String(buffer, 0, 0, len));
byte[]buffer=新字节[237];
int len=dis.read(buffer);//再读取237个字节。
if(len<0)抛出新的EOFEException();//没有更多数据。
对于(int i=0;i
在调试器中,是否可以检查偏移量>=0和偏移量+长度b.length-off){ 抛出新的IndexOutOfBoundsException();
检查的条件之一无效。

您将获得IndexOutOfBoundsException-如果

  • 偏移量为负

  • len是负数

  • len大于buffer.length-off

第3点的例子:

如果输入文件中有500个字符或1500个字符,则以下程序将成功运行

byte[] buffer = new byte[1000];
int offset = 0;
int len = 1000;
dis.read(buffer, offset, len);            
for(int i = 0; i < buffer.length; i++) System.out.print((char)buffer[i]);
byte[]buffer=新字节[1000];
整数偏移=0;
int len=1000;
dis.read(缓冲区、偏移量、len);
对于(inti=0;i
但如果

byte[] buffer = new byte[1000];
int offset = 0;
int len = 1001;
dis.read(buffer, offset, len); 
for(int i = 0; i < buffer.length; i++) System.out.print((char)buffer[i]);
byte[]buffer=新字节[1000];
整数偏移=0;
int len=1001;
dis.read(缓冲区、偏移量、len);
对于(inti=0;i

检查两种情况下的长度值。

您将获得IndexOutOfBoundsException-如果

  • 偏移量为负

  • len是负数

  • len大于buffer.length-off

第3点的例子:

如果输入文件中有500个字符或1500个字符,则以下程序将成功运行

byte[] buffer = new byte[1000];
int offset = 0;
int len = 1000;
dis.read(buffer, offset, len);            
for(int i = 0; i < buffer.length; i++) System.out.print((char)buffer[i]);
byte[]buffer=新字节[1000];
整数偏移=0;
int len=1000;
dis.read(缓冲区、偏移量、len);
对于(inti=0;i
但如果

byte[] buffer = new byte[1000];
int offset = 0;
int len = 1001;
dis.read(buffer, offset, len); 
for(int i = 0; i < buffer.length; i++) System.out.print((char)buffer[i]);
byte[]buffer=新字节[1000];
整数偏移=0;
int len=1001;
dis.read(缓冲区、偏移量、len);
对于(inti=0;i

检查两种情况下的长度值。

如果将流读入缓冲区数组,则偏移量和len必须始终为:

offset = 0;
len = buffer.length();
这些参数指定数据放在缓冲区中的位置,而不是从流中读取哪些数据。流是连续读取的(或者如何拼写?)

如果你总是打电话:

buffer = new byte[256];
dis.read(buffer, 0, 256);
这将发生: 在第一次调用之前,Streamposition(返回的下一个字节的位置)是0

  • 调用后的Streamposition=256,缓冲区包含字节0-255
  • 调用后的Streamposition=512,缓冲区包含字节256-511
  • dis.reset()

  • Streamposition再次为0

    此代码仅将字节256-511从流读入缓冲区:

    byte[] buffer = new byte[512];
    dis.skip(256);
    dis.read(buffer, 0, 256);
    
    请注意,缓冲区的最后256个字节没有被填满。这是读取(byte[],int,int)和读取(byte[])之间的区别之一

    以下是一些链接,介绍了流的概念和读取方法的用法:

    如果将流读入缓冲区数组,则偏移量和len必须始终为:

    offset = 0;
    len = buffer.length();
    
    这些参数指定数据放在缓冲区中的位置,而不是从流中读取哪些数据。流是连续读取的(或者如何拼写?)

    如果你总是打电话:

    buffer = new byte[256];
    dis.read(buffer, 0, 256);
    
    这将发生: 在第一次调用之前,Streamposition(返回的下一个字节的位置)是0

  • 调用后的Streamposition=256,缓冲区包含字节0-255
  • 调用后的Streamposition=512,缓冲区包含字节256-511
  • dis.reset()

  • Streamposition再次为0

    此代码仅将字节256-511从流读入缓冲区:

    byte[] buffer = new byte[512];
    dis.skip(256);
    dis.read(buffer, 0, 256);
    
    请注意,缓冲区的最后256个字节没有被填满。这是读取(byte[],int,int)和读取(byte[])之间的区别之一

    以下是一些链接,介绍了流的概念和读取方法的用法:

    偏移量是237,len也是237。所以缓冲长度是237。不是吗