import java.util.Scanner; import java.io.*; /** * This is a quick and dirty program to reformat a .ppm file generated by * the GIMP so that the RGB values are in three columns instead of just one. * * Input is a filename with no extension. The program adds the extension ".ppm" * to create the input file name, and creates an additional, reformatted output file * with "-3col.ppm" tacked onto the filename. * */ public class FixPPMFile { public static void main(String[] args) { //Get filename from commandline or use hardcoded name String filename = ""; if (args.length > 0) filename = args[0]; else { filename = "fixme"; // could also prompt user for another filename here instead ... } String infile = filename + ".ppm"; String outfile = filename + "-3col.ppm"; Scanner in = null; try { in = new Scanner(new File(infile)); } catch (Exception e) { System.err.println("Can't open input file: " + infile); System.exit(-1); } PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(outfile)); } catch (Exception e) { System.err.println("Can't open output file: " + outfile); System.exit(-1); } // Read the first four header lines and output these verbatim for (int i = 0; i < 4; i++) { String line = in.nextLine(); pw.println(line); } // Add some comments so students don't edit the wrong thing when using a // spreadsheet pw.println("# Do not edit above this line!"); pw.println("# Below this line are Red Green Blue columns"); // Read the rest of the data in the file, one value per line, // outputting the values 3 to a line. int count = 0; while (in.hasNextLine()) { pw.print(in.nextLine()); if (count == 2) { pw.println(); count = 0; } else { pw.print(" "); count++; } } // All done, clean up in.close(); pw.close(); } }