A good answer might be:

  • How does the loop end?
    • When end-of-file is encountered.
  • How should this be handled?
    • With a try/catch structure.

End of File

This loop is the familiar read-until-end-of-file idea which is best done using try{} and catch{} blocks.


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

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

Almost all of the program is here, but there are more details to handle. The one-byte-at-a-time nature of the the loop is very inefficient.

QUESTION 16:

How (in general) can IO be made more efficient in this program?

Click Here after you have answered the question