Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reading the first 10 bytes of a file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi there, I was hoping someone might be able to help me with an issue I've been having. I'm trying to get my program to read in the first 10 bytes of a specified file. I can get it to work with the skip() but obviously this does the opposite of what I want (it removes the first 10 instead)
I've looked all over to no avail, if you can help me out, that'd be great.
You can probably see that I've tried to enter the read(b, off, len) into the code already but this just produces random characters as an output rather than the actual hex characters I want 74 65 71 etc
 
Sheriff
Posts: 22572
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

1) in.read(b, 0, 10) can read less than 10 bytes. If you're sure that your file has at least 10 bytes perhaps it's easier to use RandomAccessFile and its readFully method.

2) you're trying to read 10 bytes in an 8 byte array. I'm surprised you haven't gotten any exceptions yet.

3) you're printing the result of in.read(...) in HEX. That's the number of bytes read. You actually want to print the values in b in HEX.
 
Jonathan Murphy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:Welcome to the Ranch!

1) in.read(b, 0, 10) can read less than 10 bytes. If you're sure that your file has at least 10 bytes perhaps it's easier to use RandomAccessFile and its readFully method.

2) you're trying to read 10 bytes in an 8 byte array. I'm surprised you haven't gotten any exceptions yet.

3) you're printing the result of in.read(...) in HEX. That's the number of bytes read. You actually want to print the values in b in HEX.



Hi, thanks for responding so quickly.

@2) I know, that was just an accidental part I left in when I was messing around
@3) That's what I'm trying to work out at the moment. I've got some advice on using encodeHexString(byte[] data) because you can't just print the value of b directly as there is a byte-"toHex" issue of some kind. Not exactly sure how it works though, any chance you understand and could explain how/where it needs declaring?
 
Rob Spoor
Sheriff
Posts: 22572
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know how to convert a single (positive) value to a HEX string; you're using that already (but on the wrong value). You probably know how to do that on an entire array. So that leaves one problem - getting positive values from the bytes.

That's where a bit of bitwise operations come in. Hint: you need to take the byte's bit pattern and convert it into an int, using zeros for all the bits that can't fit into an int. It involves value 0xFF, a.k.a. the bit pattern 1111 1111.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic