// MyFileCopier004.java // this version ALWAYS forces a new-line if at // the end of EACH input line. // // See: Always FORCE a N/L note below.... // // input [0] (arg #1) is the inFile (ie: the source file) // creates: inFile.OUT // // [1] (arg #2) control! -- ONLY one option is allowed // // NUM - number the file 001, 002, ... // // FOLIO - cut each line to about 40 chars // wide, and indent the block by // 5 spaces. // If a NEW LINE is encountered, // the current line is finished, // and a blank line inserted, etc. // (These are also known as the // Duchamp/Essray/Clark parameters // or Dek for short - as in DeKlein // Hawkes) // // // NOTE: While this program appears to work on // simple (8-bit ascii) text files, it // FAILS misserably on .jpg etc files.... hmm // where's c's fopen() 'rb', when you need it???) // // NOTE: look at MyFileCopier004b.java (b - as in what // a boob i am! // i had to learn the hard way about string compares, // and the toUpperCase() method !!! // // see also: http://pkda2001-java.angelfire.com/java-topics.html // nite all, // Frank. // // To test, it i used the file "g3" (from a review of LoveLock's // Gaia hypothesis and such... http://erg.ucd.ie/arupa/references/gaia.html // // Using word (wonder word, indubbitablly)... // Pages Words Chars w/spaces Para's Lines // g3-word-manual.doc 7 1591 7913 9488 101 181 // g3-out-auto-folio.doc 12 1591 7913 10954 248 289 // import java.lang.*; import java.io.*; import java.util.StringTokenizer; class MyFileCopier004 { public PrintWriter outFile; public String outFileName; //------------------------------------------------------------- public void open_and_copy_file (String inFileName, String processing_option) //------------------------------------------------------------- // Copy strings from inFile to outFile (use fname.OUT as output) { BufferedReader inFile; String nextLine; String lineBuffer, hold_the_rest; Integer colCounter; Integer lineNum = 1; Integer DekLimit = 40; // naievely assume... lineBuffer = " "; // indent 5 to start all... hold_the_rest = ""; colCounter = 0; try //----------------------------------- try { inFile = new BufferedReader (new FileReader (inFileName)); outFileName = new String(); outFileName = inFileName + ".out"; PrintWriter outFile = new PrintWriter (new FileWriter (outFileName)); nextLine = inFile.readLine(); while (nextLine != null) { // NOTE: For now, the NUM and FOLIO options are // mutually exclusive. Hmmmm, needs a bit o' thinkin? mayhaps...? // if (processing_option.compareTo("NUM") == 0) // do we number?? { // yes! if (lineNum < 10) outFile.print (" 000"); else if (lineNum < 100) outFile.print ( " 00"); else if (lineNum < 1000) outFile.print ( " 0"); else outFile.print ( " "); outFile.print (lineNum); outFile.print (" "); lineNum++; } //------------------------ F O L I O ----------------------------- // note: will need to CLONE / COPY any blank lines when encountered. // ie, output whatever is already in linBuffer, and then // echo the blank line(s).... // if (processing_option.compareTo("FOLIO") == 0) // do we Deklein it? { // ?? hmmm lineBuffer = hold_the_rest + " " + lineBuffer; StringTokenizer My_tokenizer = new StringTokenizer (nextLine); if (My_tokenizer.hasMoreTokens() == false) { outFile.println (" "+ lineBuffer); outFile.println (" "); // echo the blank line lineBuffer = ""; lineNum++; } else //------- YES: a non-blank line --------------------------- { while (My_tokenizer.hasMoreTokens()) { lineBuffer = lineBuffer + " " + My_tokenizer.nextToken(); if ((lineBuffer.length() + 5) > DekLimit) // really need { // to peak at [n+1] // output what we have.... outFile.println (" " + lineBuffer); lineBuffer = ""; } } // end of input line //--------- 004: Always FORCE a N/L note ---------------------------------------// if (lineBuffer.length() != 0) //--- 004: Always FORCE a N/L note --// outFile.println (" " + lineBuffer); //--- First flush existing --// lineBuffer = ""; //--- --// outFile.println (" "); //-----------------------------------// } //--- end of else: YES: a non-blank line ----------------------- nextLine = inFile.readLine(); lineNum++; } else { //--- only FOLIO abrdiges the normal "flush" of cur line --- outFile.println (nextLine); nextLine = inFile.readLine(); } } //--- eof on inFile --- if (processing_option.compareTo("FOLIO") == 0) // flush lineBuffer? { if (lineBuffer.length() > 0) outFile.println (" " + lineBuffer); } System.out.println ("--- file processed: " + outFileName + "\n"); outFile.close(); System.exit(0); } // end try catch (Exception e) { System.out.println ("\n*** [Line-042] writeHtmlPage: " + e.getMessage() ); System.out.println ("\n"); System.exit(1); //*** ERROR EXIT ***// } // end try/catch ---------------------------- end try } // end: open_and_copy_file() // public static void main (String[] args) { String command_option, c1; boolean command_ok; //-- BEGIN --// if (args.length < 1) { System.out.println ("\n*** Err: You must supply a file name"); System.out.println ("\n eg: java MyFileCopier004 poem01.txt"); System.out.println ("\n Format: java filename"); System.out.println (" or: java filename OPTION\n"); System.out.println (" OPTION: NUM - Enumber the lines in the file."); System.out.println (" FOLIO - Dek-folio the file (40 chars, +5 indented)."); System.out.println (" "); System.exit(1); // ie, unix std: 0 = ok, any other means err. } //-- let's go! if (args.length < 2) c1 = "NIL"; else c1 = args[1]; command_option = c1.toUpperCase(); // System.out.println ("<><> debug [ 101]: command_option: >" + command_option + "<\n"); command_ok = false; if ( (command_option.equals("FOL")) || (command_option.equals("FOLIO")) ) { System.out.println ("--- The text will be Dek-Folioed."); command_option = "FOLIO"; command_ok = true; } if (command_option.equals("NUM")) { System.out.println ("--- The lines will be numbered."); command_ok = true; } if (command_option.equals("NIL")) command_ok = true; //-- no option is ok. if (command_ok == false) System.out.println ("\n*** WARN: Unknown OPTION: " + command_option + "\n"); if (command_option.equals("NIL") == false) System.out.println ("--- command_option set: >" + command_option + "<\n"); MyFileCopier004 fileWriter = new MyFileCopier004(); fileWriter.open_and_copy_file (args[0], command_option); } // end main() } // end: class MyFileCopier004