Reading Data from a File
9. Skeleton Program
Answer:
Yes.
Skeleton Program
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"); } } |
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:
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?