Which class is better for reading binary data?

Discussion in 'Java' started by Royan, Apr 13, 2008.

  1. Royan

    Royan Guest

    Recently I have faced a problem of choosing a proper class from
    java.io package (my goal is to use standard java.io package). So I had
    to find an alias of BufferedReader but designed specifically for
    reading raw binary data. I wonder what could you advice me on that?
    Royan, Apr 13, 2008
    1. Advertisements

  2. Royan

    Arne Vajhøj Guest

    The class for reading binary data is DataInputStream (any InputStream
    are OK for reading bytes, but if you need to read other simple data
    types then DataInputStream is it).

    BufferedReader are for reading lines of text.

    Arne Vajhøj, Apr 13, 2008
    1. Advertisements

  3. Don't use BufferedReader (I meand Reader) because it converts bytes
    from local stream to unicode characters and so can't be used for raw

    It's not clear what you try to achieve bit it it looks like yo need
    buffer for speed (BufferedInputStream) and raw data access
    (DataInputStream). Java is wonderful language and it can combine and
    create stream exactly what you need. So, based on what I think it
    would be like this:

    new DataInputStream(new BufferedInputStream(new

    So you will have speed of buffer and data conversion ability of data
    stream to read raw data.
    And, of course, you always can use "low" level of any stream to read
    real raw data byte by byte or all of them in one buffer.

    Alex.From.Ohio.Java, Apr 13, 2008
  4. Royan

    Royan Guest

    Arne and Lew thanks!
    Alex, I think you have answered my question. I'll use a combination of
    DataInputStream, BufferedInputStream and FileInputStream just as
    you've suggested.
    Royan, Apr 13, 2008
  5. Royan

    Roedy Green Guest

    see http://mindprod.com/applet/fileio.html
    to generate you sample code. Use either a DataStream or an
    Roedy Green, Apr 14, 2008
  6. Royan

    Roedy Green Guest

    Your choices are DataInputStream for big-endian
    LEDataInputStream for little-endian
    and possibly nio for low level high performance.

    see http://mindprod.com/applet/fileio.html
    for sample code for DataInputStream and LEDataInputStream
    Roedy Green, Apr 24, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.