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

K

kvnsmnsn

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_
 
V

vic

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
 
O

Oliver Wong

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
 
K

kvnsmnsn

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_
 
O

Oliver Wong

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
 
K

kvnsmnsn

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>");
}
}
}
 
O

Oliver Wong

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>");
}
}
}
 
K

kvnsmnsn

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_
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top