Skeleton Program

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:

Yes.

Skeleton Program

The program must work correctly with an empty file. This may seem dumb, but there is no reason to have a program break when it is used in an unexpected fashion. Here is the copy program with pieces left out:

class CopyMaker
{
  String sourceName, destName;
  BufferedReader source;
  PrintWriter dest;
  String line;

  CopyMaker ( String source, String dest )
  {
    sourceName = source;
    destName   = dest;
  }
  
  private boolean openFiles()  // return true if files open, else false
  {
  }

  private void copyFiles()
  {
  }

  private void closeFiles() 
  {
  }

  public static void main ( String[] args ) 
  {
    if ( args.length == 3 && args[1].toUpperCase().equals("TO") )
    {
      CopyMaker cp = new CopyMaker( args[0], args[2] );
      if ( cp.openFiles() )
      {
        cp.copyFiles() ; 
        cp.closeFiles() ;
       }
    }
    else
      System.out.println("java CopyTextFile source to destination");
  }

}

Rather than write one big main() it is convenient to define an object whose several methods implement the steps in copying files. Potentially the object could be used as a component of a larger program.

The main() method collects information from the user. If the information looks correct, it creates a CopyMaker object and calls its methods.

QUESTION 9:

If the files open successfully but there is a problem in making the copy, should the program close the files?