Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
kvnsmnsn@hotmail.com - 04 Jul 2006 15:47 GMT
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_
vic - 04 Jul 2006 17:18 GMT
> 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
[quoted text clipped - 7 lines]
> 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
Oliver Wong - 04 Jul 2006 17:22 GMT
> 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
[quoted text clipped - 7 lines]
> 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
kvnsmnsn@hotmail.com - 05 Jul 2006 19:53 GMT
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_
Oliver Wong - 05 Jul 2006 21:11 GMT
> Thanks to vic and Oliver Wong for the answers given to my question.
>
[quoted text clipped - 3 lines]
> 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
kvnsmnsn@hotmail.com - 05 Jul 2006 23:02 GMT
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>");
   }
 }
}
Oliver Wong - 05 Jul 2006 23:26 GMT
>     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
>
[quoted text clipped - 49 lines]
>  }
> }
kvnsmnsn@hotmail.com - 06 Jul 2006 05:21 GMT
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_


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.