aspose file tools
The moose likes I/O and Streams and the fly likes Reading a field in a Binary file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "Reading a field in a Binary file" Watch "Reading a field in a Binary file" New topic
Author

Reading a field in a Binary file

Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 153
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!!!


SCJP/OCPJP 6 | SCWCD/OCPJWCD 5 | OCPJBCD in progress
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 32768
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.


Android appsImageJ pluginsJava web charts
Denise Advincula
Ranch Hand

Joined: Jan 01, 2007
Posts: 153
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
Saloon Keeper

Joined: Oct 27, 2005
Posts: 18365

Replied to that thread with the correct answer.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
 
subject: Reading a field in a Binary file
 
Threads others viewed
Download through servlets
FilteInputStream and byte
Class Loading from a file
175: warning: BASE64Encoder is internal proprietary API and m ay be removed in a future release
HTTP receiving problems with non text files.
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com