Opening the Files

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:

Exceptions may be thrown when the files are opened.

Opening the Files

The constructors for FileInputStream and for FileOutputStream throw exceptions which need to be caught. This calls for an outer try{} block:

DataInputStream  instr;
DataOutputStream outstr;
. . . . 
try
{
  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 outer try{} block encloses the constructors and the IO loop.

QUESTION 18:

What might go wrong inside the outer try{} block which would cause an exception?