import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Copies a file containing JavaFX color names and the corresponding RGB values to a new
 * file containing just the names.
 *
 * Using line-oriented character streams and try-with-resources.
 *
 * @author Drue Coles
 */
public class ColorCopy3 {

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

        String inFileName = "colors.txt";
        String outFileName = "output.txt";
        
        try (BufferedReader input = new BufferedReader(new FileReader(inFileName));
                PrintWriter output = new PrintWriter(new FileWriter(outFileName))) {

            final int MAX = 80; // bound on number of characters per line of output
            String text;
            String rowOfColors = "";

            while ((text = input.readLine()) != null) {

                // Extract color name from input and format in wide column.
                String colorName = text.substring(0, text.indexOf(" "));
                rowOfColors += String.format("%22s", colorName);

                // If row exceeds MAX characters, start a new row.
                if (rowOfColors.length() > MAX) {
                    output.println(rowOfColors);
                    rowOfColors = "";
                }
            }
            output.println(rowOfColors);
        }
    }
}