import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Copies a file containing JavaFX color names and the corresponding RGB values to a new
 * file containing just the names.
 *
 * Using character streams.
 *
 * @author Drue Coles
 */
public class ColorCopy2 {

    public static void main(String[] args) throws IOException {

        FileReader input = null;
        FileWriter output = null;
        String inFileName = "colors.txt";
        String outFileName = "output.txt";

        try {
            input = new FileReader(inFileName);
            output = new FileWriter(outFileName);

            int charVal;
            while ((charVal = input.read()) != -1) {

                boolean readingUpperCaseLetter = true;
                while ((charVal = input.read()) != -1) {

                    if (Character.isUpperCase(charVal)) {

                        // If not previously reading an uppercase letter, then this is the
                        // start of a new color name, so output a newline character.
                        if (!readingUpperCaseLetter) {
                            output.write('\n');
                            readingUpperCaseLetter = true;
                        }

                        // The current character is an uppercase letter so it is part of a 
                        // color name and is written to the output.
                        output.write(charVal);
                    } else {
                        readingUpperCaseLetter = false;
                    }
                }

            }
        } catch (FileNotFoundException e) {
            System.out.printf("Could not find %s in working directory.%n", inFileName);
        } finally {
            if (input != null) {
                input.close();
            }
            if (output != null) {
                output.close();
            }
        }
    }
}