Reading and Writing <byte> and <char> Values from and to Disk Quickly

Discussion in 'Java' started by kvnsmnsn@hotmail.com, Jul 4, 2006.

  1. Guest

    I'm writing a Java program that needs to store <byte> values in a file
    and another program that needs to read those <byte> values from such
    files quickly. The second program also needs to be able to write
    16-bit unsigned values to another file quickly and read those 16-bit
    values from such files quickly.

    After looking at a few types I've come to the conclusion that <char>
    values would be a good match for my 16-bit unsigned values. So is
    there a way in Java to read and write <byte> values to disk quickly,
    and to read and write <char> values to disk quickly? Any information
    on this would be greatly appreciated.

    ---Kevin Simonson

    "You'll never get to heaven, or even to LA,
    if you don't believe there's a way."
    from _Why Not_
     
    , Jul 4, 2006
    #1
    1. Advertising

  2. vic Guest

    Re: Reading and Writing <byte> and <char> Values from and to DiskQuickly

    wrote:
    > I'm writing a Java program that needs to store <byte> values in a file
    > and another program that needs to read those <byte> values from such
    > files quickly. The second program also needs to be able to write
    > 16-bit unsigned values to another file quickly and read those 16-bit
    > values from such files quickly.
    >
    > After looking at a few types I've come to the conclusion that <char>
    > values would be a good match for my 16-bit unsigned values. So is
    > there a way in Java to read and write <byte> values to disk quickly,
    > and to read and write <char> values to disk quickly? Any information
    > on this would be greatly appreciated.


    The use of a char seems a little borderline, but ok, this would work.
    Note that bytes are signed in java, and there is no unsigned equivalent
    unfortunately. This may be an issue in your case. You can probably adapt
    your arithmetic to take this into account, ie using two's complement.

    To read data the fastest way would be to use a FileInputStream with a
    big buffer, a few kilobytes (adjust to your needs). BufferedInputStream
    is good too but slower than doing it by hand. For writing, use the same
    principle. For simplicity you'd best use a BufferedOutputStream however.
    If you intend to store all the values in RAM before writing, use an
    ObjectOutputStream and serialize it all in one go.

    > ---Kevin Simonson
    >
    > "You'll never get to heaven, or even to LA,
    > if you don't believe there's a way."
    > from _Why Not_
    >


    vic
     
    vic, Jul 4, 2006
    #2
    1. Advertising

  3. Oliver Wong Guest

    <> wrote in message
    news:...
    > I'm writing a Java program that needs to store <byte> values in a file
    > and another program that needs to read those <byte> values from such
    > files quickly. The second program also needs to be able to write
    > 16-bit unsigned values to another file quickly and read those 16-bit
    > values from such files quickly.
    >
    > After looking at a few types I've come to the conclusion that <char>
    > values would be a good match for my 16-bit unsigned values. So is
    > there a way in Java to read and write <byte> values to disk quickly,
    > and to read and write <char> values to disk quickly? Any information
    > on this would be greatly appreciated.


    Are BufferedInputStream/BufferedOutputStream wrapped around a
    FileInputStream/FileOutputStream not quick enough for your purposes?

    - Oliver
     
    Oliver Wong, Jul 4, 2006
    #3
  4. Guest

    Thanks to vic and Oliver Wong for the answers given to my question.

    It looks like I want to use the <write()> method from class
    <FileOutputStream> to write my <byte>s and the <read()> method from
    class <FileInputStream> to read my <byte>s. What do I lose by using
    just <FileOutputStream> and <FileInputStream> by themselves, and not
    using <BufferedOutputStream> and <BufferedInputStream> at all?

    Also, these classes appear to work exclusively with bytes. Is there
    any way to directly write <char>s to disk and directly read <char>s
    from disk, or do I have to break each <char> into two <byte>s before
    writing and reassemble them after reading?

    ---Kevin Simonson

    "You'll never get to heaven, or even to LA,
    if you don't believe there's a way."
    from _Why Not_
     
    , Jul 5, 2006
    #4
  5. Oliver Wong Guest

    <> wrote in message
    news:...
    > Thanks to vic and Oliver Wong for the answers given to my question.
    >
    > It looks like I want to use the <write()> method from class
    > <FileOutputStream> to write my <byte>s and the <read()> method from
    > class <FileInputStream> to read my <byte>s. What do I lose by using
    > just <FileOutputStream> and <FileInputStream> by themselves, and not
    > using <BufferedOutputStream> and <BufferedInputStream> at all?


    You might lose performance. Imagine the OS is an elderly librarian. You
    ask for a given book. She slowly wanders off, and 10 minutes later, she
    comes back with your book. You then ask for a second book. She wanders off,
    and another 10 minutes later, she comes back. That took 20 minutes.
    Alternatively, you could have asked her for 2 books right away, and it might
    have only taken her 15 minutes to get the two books together for you. It's
    like that. Sort of. Maybe.

    See http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedWriter.html

    >
    > Also, these classes appear to work exclusively with bytes. Is there
    > any way to directly write <char>s to disk and directly read <char>s
    > from disk, or do I have to break each <char> into two <byte>s before
    > writing and reassemble them after reading?


    You'll have to choose an encoding to convert the chars into bytes. The
    encoding you're speaking of sort of sounds like UTF-16. Another (more
    common, I think) encoding is UTF-8.

    See
    http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStreamWriter.html

    - Oliver
     
    Oliver Wong, Jul 5, 2006
    #5
  6. Guest

    Oliver Wong posted:

    = You might lose performance. Imagine the OS is an elderly
    librarian. You
    =ask for a given book. She slowly wanders off, and 10 minutes later,
    she
    =comes back with your book. You then ask for a second book. She wanders
    off,
    =and another 10 minutes later, she comes back. That took 20 minutes.
    =Alternatively, you could have asked her for 2 books right away, and it
    might
    =have only taken her 15 minutes to get the two books together for you.
    It's
    =like that. Sort of. Maybe.

    Okay, I decided to use <BufferedOutputStream> and
    <BufferedInputStream> and wrote the program included below. I noticed
    that <BufferedInputStream> appears to have no way of recognizing the
    end of a file. Am I missing something, or do you have to know the
    size of such a file before you can know how much of it you can use?

    ---Kevin Simonson

    "You'll never get to heaven, or even to LA,
    if you don't believe there's a way."
    from _Why Not_

    ####################################################################

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;

    public class Copy
    {
    public static void main ( String[] arguments)
    {
    BufferedInputStream inStream;
    BufferedOutputStream outStream;
    int size, count;

    if (arguments.length == 3)
    { try
    { size = Integer.parseInt( arguments[ 0]);
    inStream
    = new BufferedInputStream( new FileInputStream( arguments[
    1]));
    outStream
    = new BufferedOutputStream( new FileOutputStream( arguments[
    2]));
    for (count = 0; count < size; count++)
    { outStream.write( inStream.read());
    }
    inStream.close();
    outStream.close();
    }
    catch (FileNotFoundException excptn)
    { System.err.println( "File \"" + arguments[ 1] + "\" doesn't
    exist.");
    }
    catch (IOException excptn)
    { System.err.println( "Some I/O error occurred.");
    }
    }
    else
    { System.out.println
    ( "Usage is\n java Copy <source-size> <source>
    <destination>");
    }
    }
    }
     
    , Jul 5, 2006
    #6
  7. Oliver Wong Guest

    <> wrote in message
    news:...
    >
    > Okay, I decided to use <BufferedOutputStream> and
    > <BufferedInputStream> and wrote the program included below. I noticed
    > that <BufferedInputStream> appears to have no way of recognizing the
    > end of a file. Am I missing something, or do you have to know the
    > size of such a file before you can know how much of it you can use?


    read() returns -1 when it reaches the end of the file.

    >
    > ---Kevin Simonson
    >
    > "You'll never get to heaven, or even to LA,
    > if you don't believe there's a way."
    > from _Why Not_
    >
    > ####################################################################
    >
    > import java.io.FileInputStream;
    > import java.io.FileOutputStream;
    > import java.io.BufferedInputStream;
    > import java.io.BufferedOutputStream;
    > import java.io.FileNotFoundException;
    > import java.io.IOException;
    >
    > public class Copy
    > {
    > public static void main ( String[] arguments)
    > {
    > BufferedInputStream inStream;
    > BufferedOutputStream outStream;
    > int size, count;
    >
    > if (arguments.length == 3)
    > { try
    > { size = Integer.parseInt( arguments[ 0]);
    > inStream
    > = new BufferedInputStream( new FileInputStream( arguments[
    > 1]));
    > outStream
    > = new BufferedOutputStream( new FileOutputStream( arguments[
    > 2]));
    > for (count = 0; count < size; count++)
    > { outStream.write( inStream.read());
    > }
    > inStream.close();
    > outStream.close();
    > }
    > catch (FileNotFoundException excptn)
    > { System.err.println( "File \"" + arguments[ 1] + "\" doesn't
    > exist.");
    > }
    > catch (IOException excptn)
    > { System.err.println( "Some I/O error occurred.");
    > }
    > }
    > else
    > { System.out.println
    > ( "Usage is\n java Copy <source-size> <source>
    > <destination>");
    > }
    > }
    > }
     
    Oliver Wong, Jul 5, 2006
    #7
  8. Guest

    Oliver Wong posted:

    => Okay, I decided to use <BufferedOutputStream> and
    => <BufferedInputStream> and wrote the program included below. I
    noticed
    => that <BufferedInputStream> appears to have no way of recognizing the
    => end of a file. Am I missing something, or do you have to know the
    => size of such a file before you can know how much of it you can use?
    =
    =read() returns -1 when it reaches the end of the file.

    Thanks! This gives me everything I need.

    ---Kevin Simonson

    "You'll never get to heaven, or even to LA,
    if you don't believe there's a way."
    from _Why Not_
     
    , Jul 6, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Richard
    Replies:
    11
    Views:
    1,083
    Adam Maass
    Feb 1, 2006
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,057
    Ian Collins
    May 9, 2006
  3. Navkirat Singh

    Writing byte stream as jpeg format to disk

    Navkirat Singh, Aug 26, 2010, in forum: Python
    Replies:
    12
    Views:
    1,117
    Robert Kern
    Aug 28, 2010
  4. Robert Kern
    Replies:
    3
    Views:
    464
    Bryan
    Aug 27, 2010
  5. KevinSimonson
    Replies:
    12
    Views:
    809
    Paul N
    Oct 20, 2010
Loading...

Share This Page