import java.io.*;

    // Combine the files whose names are given to combineFiles to
    // the file named in the constructor.
class CombineFiles {
    public CombineFiles(String whereTo){
        // Keep a copy of the name of the combined file.
        destination = whereTo;
    }

        // Write these files to the `destination' file.
    public void combineFiles(String[] filesToCombine){
        // Where the files will be appended to.
        FileWriter outputFile = null;
        try{
            checkReadability(filesToCombine);
            // Append to the destination file.
            outputFile = new FileWriter(getDestination(),true);
            for(int n = 0; n < filesToCombine.length; n++){
                // Copy the next file into the destination.
                try{
                    FileReader inputFile = new FileReader(filesToCombine[n]);
                    copyFile(inputFile,outputFile);
                    inputFile.close();
                }
                catch(FileNotFoundException e){
                    System.out.println("Unable to open "+filesToCombine[n]);
                }
                catch(IOException e){
                    System.out.println(e.toString());
                }
            }
        }
        catch(IOException e){
            System.out.println(e.toString());
        }
        finally{
            if(outputFile != null){
                try{
                    outputFile.close();
                }
                catch(IOException e){
                    System.out.println("Unable to close "+getDestination());
                }
            }
        }
    }

        // Throw a FileNotFoundException if any of the files
        // is readable or does not exist.
    public void checkReadability(String[] filesToRead)
                                        throws FileNotFoundException {
        // Build up an error message for bad files.
        StringBuffer notFound = new StringBuffer();
        final int numFiles = filesToRead.length;
        for(int i = 0; i < numFiles; i++){
            File details = new File(filesToRead[i]);
            if(!details.exists()){
                notFound.append(filesToRead[i]);
                notFound.append(" does not exist. ");
            }
            else if(!details.canRead()){
                notFound.append(filesToRead[i]);
                notFound.append(" is not readable. ");
            }
            // else - ok.
        }
        if(notFound.length() != 0){
            throw new FileNotFoundException(notFound);
        }
    }

    // Copy everything from inputFile to outputFile.
    private void copyFile(FileReader inputFile,FileWriter outputFile)
                                            throws IOException{
        // Provide space for a small number of characters to be read.
        final int bufferSize = 1024;
        char[] buffer = new char[bufferSize];
        // Read the first chunk of characters.
        int numberRead = inputFile.read(buffer);
        // Keep going as long as something was read.
        while(numberRead > 0){
            // Write out what was read.
            outputFile.write(buffer,0,numberRead);
            // Read the next chunk, if any.
            numberRead = inputFile.read(buffer);
        }
        outputFile.flush();
    }

        // Return a copy of the destination filename.
    public String getDestination(){
        return destination;
    }

    // The name of the combined file.
    private final String destination;
}
