• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading float, short from InputStream  RSS feed

 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I'm just wondering how to read data types such as float, double, short and long from an InputStream that is reading a file. I've been using RandonAccessFile to do this so far, given that it has methods such as readFloat() which InputStream doesn't have...now I need to use InputStream to read data in a similar way because I'm reading from inside a zip file. The read() method of InputStream returns a byte or array of bytes, but how do I convert these into the aforementioned types? For example if I want to read a short (2 bytes), I presume I need to grab 2 bytes from the input stream, but than I'm stuck...how is that converted into a short value??? Any help greatly appreciated.
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a DataInputStream.

A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way.

One would normally use a DataOutputStream to write the data, but it sounds like it should work in your case.
 
Ben Wood
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe. I apologise for my lack of IO API knowledge
 
Joe Ess
Bartender
Posts: 9426
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No worries. That's why we're here.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!