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