• Post Reply Bookmark Topic Watch Topic
  • New Topic

exception when using FileInputStream public int read(byte[] b, int off, int len)

 
mitka daradidova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am getting exception when using FileInputStream public int read(byte[] b, int off, int len).
fin = new RandomAccessFile(inputFile, "r");
byte[] b = new byte[5];
int x = fin.read(b, 5, 10);

Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.io.RandomAccessFile.readBytes(Native Method)
at java.io.RandomAccessFile.read(Random AccessFile.java:215)
at Sort.execute(Sort.java:45)
at Sort.main(Sort.java:35)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Welcome to JavaRanch!
Why are you calling this a FileInputStream problem when you're using RandomAccessFile, an unrelated class?
The three arguments to "read" are the buffer to read into, the index of the first location in that buffer where the data should go, and and number of bytes to read. Since your buffer is only 5 bytes long, both the start index (5) and the count (10) you're using are wrong.
I think since you're using RandomAccessFile that what you're interested in doing is reading 5 bytes starting from the fifth byte in the file. To do that, you have to move the file pointer to byte 5:
 
mitka daradidova
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Ernest.
Sorry for the incorrect wording.
I know understand the role of the second argument (and my mistake):
"the index of the first location in that buffer where the data should go".
Mitka
[ November 12, 2003: Message edited by: mitka daradidova ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!