File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Read/Write ints using 2bytes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read/Write ints using 2bytes" Watch "Read/Write ints using 2bytes" New topic

Read/Write ints using 2bytes

Lora Tully

Joined: Nov 29, 2004
Posts: 8
Hi. I was wondering if anyone can help me read and write one interger using two bytes. I have an interger between 500-800 that I need to write. Normally I would use...


But as 500 is above one byte (256) I was wondering how to write this, and then later read it back.
As since one int would be spread over two bytes, I have no idea how to read it, normally I would use


And I have no idea how to do this. Can anyone help me please?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Try DataOutputStream.writeShort() and DataInputStream.readShort()

Mike Gershman
SCJP 1.4, SCWCD in process
Lora Tully

Joined: Nov 29, 2004
Posts: 8
Thanks that works great!
I agree. Here's the link:
subject: Read/Write ints using 2bytes
It's not a secret anymore!