Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Reading and Writing <byte> and <char> Values from and to Disk Quickly

Reply
Thread Tools

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

 
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      07-04-2006
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_

 
Reply With Quote
 
 
 
 
vic
Guest
Posts: n/a
 
      07-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Oliver Wong
Guest
Posts: n/a
 
      07-04-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

 
Reply With Quote
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      07-05-2006
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_

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      07-05-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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/...redWriter.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/...eamWriter.html

- Oliver

 
Reply With Quote
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      07-05-2006
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>");
}
}
}

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      07-05-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> 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>");
> }
> }
> }


 
Reply With Quote
 
kvnsmnsn@hotmail.com
Guest
Posts: n/a
 
      07-06-2006
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_

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Threading model for reading 1,000 files quickly? leegee@gmail.com Java 4 10-03-2012 11:58 AM
Writing a <char> Array to Disk and Reading it in again KevinSimonson C++ 12 10-20-2010 09:45 PM
Writing Binaries to and Reading Binaries from Disk KevinSimonson Java 17 09-28-2009 05:40 PM
writing large files quickly rbt Python 36 01-29-2006 01:01 PM
reading from a file without actually writing into its hard disk copy Jeevan C Programming 4 10-21-2003 07:17 AM



Advertisments