// This program obtain two filenames from the command line
// and then copies lines from the first file to the second.
// The execution is aborted if a line of the input file is empty.
// A finally clause is used to ensure the output file is closed.
// Barry Cornelius, 17 June 2000
import java.io. BufferedReader;
import java.io. BufferedWriter;
import java.io. FileReader;
import java.io. FileWriter;
import java.io. IOException;
import java.io. PrintWriter;
public class CopyFinally
{ //BJCHEREFIRST
   public static void main(final String[] pArgs) throws IOException
   {
      if (pArgs.length!=2)
      {
         System.out.println("Usage: java CopyFinally inputname outputname");
         System.exit(1);
      }
      final boolean tSuccessfulCopy = iCopyFileStopEmpty(pArgs[0], pArgs[1]);
      if ( ! tSuccessfulCopy )
      {
         System.out.println("Error: there was a blank line in " + pArgs[0]);
      }
   }

   private static boolean iCopyFileStopEmpty(final String pInputFilename,
                                             final String pOutputFilename)
                                                           throws IOException
   {
      PrintWriter tOutputHandle = null;
      try
      {
         tOutputHandle = new PrintWriter(
                       new BufferedWriter(new FileWriter(pOutputFilename)));
         final BufferedReader tInputHandle =
                       new BufferedReader(new FileReader(pInputFilename));
         while (true)
         {
            final String tLine = tInputHandle.readLine();
            if (tLine==null)
            {
               break;
            }
            if (tLine.length()==0)
            {
               return false;
            }
            tOutputHandle.println(tLine);
         }
      }
      finally
      {
         tOutputHandle.close();
      }
      return true;
   }
}
