A good answer might be:

The program could be modified so that it opens the input file for appending, using the constructor:

         
FileWriter(String fileName, boolean append) 

C-style Input Loop

Here is the loop from the copyFile() method. The logic of the loop is correct and it works efficiently.

       line = source.readLine();
       while ( line != null )
       {
         dest.write(line);
         dest.newLine();
         line = source.readLine();
       }

Here is the same loop written in a style that is commonly used with the "C" programming language. This loop also works for Java:

       while ( (line = source.readLine()) != null )
       {
         dest.write(line);
         dest.newLine();
       }

The key to understanding this is to understand that an assignment statement is an expression and has a value. That value is the value that is assigned to the variable. So this: (line = source.readLine()) has a value that is non-null after a successful readLine() and null upon end-of-file. Say that the file has data in it:

1. Characters are read from the stream, and placed in a new String. The
   readLine() method returns a reference to the String.
                              |
2. The reference is           |
   assigned to line.          |
                  |           |
                --+--   ------+----------
       while ( (line  = source.readLine())   != null )
               -------+-------------------   ---+---
                      |                         |
3. The assignment statement evaluates           |
   to that (non-null) reference.                |
                                                |
4. The != operator compares non-null to null and evaluates to true.

5. The loop body executes.

This may be more bother than it is worth, but programmers familiar with C are likely to use this style, so you will see it often.

QUESTION 14:

Will this form of the loop work correctly with an empty file?

Click Here after you have answered the question