• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading a field in a Binary file

 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we get a field in a binary file without reading the whole line..? For example, I have a binary file, and one "line" or record have 10 integers on it..

In my code I use this:


This probably is not a good implementation of reading lines/records from a binary file. Because I only want the data in line[2] to be processed but I am still reading the other data... Is there a way to get the line[2] (data in index 2) directly without reading the whole binary file line/record?

I think these methods from the api are the answer but I'm not sure (and I don't want to create more arrays hopefully):

int read(byte[] b)
Reads some number of bytes from the contained input stream and stores them into the buffer array b.
int read(byte[] b, int off, int len)
Reads up to len bytes of data from the contained input stream into an array of bytes.


Hope you can shed me some light here. Thanks!!!
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, those methods don't allow you to skip over data.

If you can calculate exactly the byte location within the file, then you can use the RandomAccessFile class.
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:No, those methods don't allow you to skip over data.

If you can calculate exactly the byte location within the file, then you can use the RandomAccessFile class.



Thanks! I used RandomAccessFile now, but I encountered some problems here. I hope you can help me again... Thank you!
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replied to that thread with the correct answer.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!