Buffering

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Answer:

By using buffered input and buffered output.

Buffering

The stream types BufferedInputStream and BufferedOutputStream each put a buffer between the program and the disk. The program logic deals with single bytes. However, the actual IO is done more efficiently.


DataInputStream  instr;
DataOutputStream outstr;
. . . .
instr = 
  new DataInputStream(
    new BufferedInputStream(
      new FileInputStream( args[0] )));

outstr = 
  new DataOutputStream(
    new BufferedOutputStream(
      new FileOutputStream( args[2] )));

try
{
  int data;
  while ( true )
  {
    data = instr.readUnsignedByte() ;
    outstr.writeByte( data ) ;
  }
}

catch ( EOFException  eof )
{
  outstr.close();
  instr.close();
  return;
}

The names of the files come from the command line. The name of the file to be copied is argument number 0; the copy is argument number 2 (the word "to" is argument number 1).

QUESTION 17:

But now there is something else that needs to be handled. What is it?