I am trying to figure out how to get to a specific byte in a binary file using java. I’ve done a ton of reading on byte level operations and have gotten myself thoroughly confused. Right now I can loop through a file, as in the code below, and tell it to stop at the byte I want. But I know that this is ham-fisted and there is a ‘right’ way to do this.
So for example if I have a file and I need to return the byte from off-set 000400 how can I a get this from a FileInputStream?
public ByteLab() throws FileNotFoundException, IOException {
String s = "/Volumes/Staging/Imaging_Workflow/B.Needs_Metadata/M1126/M1126-0001.001";
File file = new File(s);
FileInputStream in = new FileInputStream(file);
int read;
int count = 0;
while((read = in.read()) != -1){
System.out.println(Integer.toHexString(count) + ": " + Integer.toHexString(read) + "\t");
count++;
}
}
Thanks
You need
RandomAccessFilefor the job. You can set the offset by theseek()method.