Homework: Java File I/O
This homework is necessary preparation for the lab. Make sure you type your code and you bring the file to the lab so that you will not have to waste time entering it during the lab.
- Write a
mainprogram that copies a given text file into another file usingSimpleReaderto read the input file andSimpleWriterto write the output file. The names of the input text file to be copied and of the destination file where the copy is to be saved are provided as command-line arguments. Assume that appropriate arguments will be provided and no error checking is necessary. The command-line arguments are accessible by your main program through theString[] argsarray parameter to themainmethod. If you need more details about command-line arguments, see Section 7.3, Command Line Arguments, in Java for Everyone. - Rewrite the file-copying
mainprogram using only the standardjava.ioclasses discussed in class . Assume that appropriate arguments will be provided and no error checking is necessary. Do not handle the possibleIOExceptions but simply declare thatmainmay throw anIOException. - Copy and modify the previous
mainprogram so that it handles all possibleIOExceptions andmainwill not throw anyIOException. Output meaningful error message(s) if anIOExceptionoccurs.
Additional Questions
- Copy and modify the previous
mainprogram so that it also includes appropriate error checks on the command line arguments, e.g., missing arguments, missing input file, non-readable input file, etc. (Thejava.io.Fileclass provides helpful methods to check some of the possible errors.)