title: Using while loop to write a file
body: <p>This is my first post so i apologize if it's not the best format. I'm student writing a code to import a document, read a line in the document and then reverse the letters in each word. The new word will be printed into a new file. For example "Jon 123" would be stored and written as "321 noJ". I have gotten the input to work but there is a problem with the writing of the line. The program is only writing the last word that is stored.</p>
  
  <p>The abridged main method code is as follows:</p>
  
  <pre><code>    //get first line of text
      line = bw.readLine();
  
      //while string is not null
      while (line != null)
      {
          System.out.println ("Processing...");   //display message to show work being done
  
          tokenLine = lineToken(line);    //tokenize string
  
          //to prevent exception from no token found
          while (tokenLine.hasMoreTokens())
          {
              word = flipWord(tokenLine);     //get next token and reverse letters
              newLine = marginCheck(word);    //store or write line depending on margin
  
              flushClose(newLine);    //write and flush buffer then close file
          }
  
  
          //move to next line in file
          line = bw.readLine();
      }
  
      flushClose(newLine);    //write and flush buffer then close file
  
      //output completion message
      System.out.println("The new file has been written.");
  </code></pre>
  
  <p>The relevant methods as follows:</p>
  
  <p>public static StringTokenizer lineToken(String line)
      {
          //local constants</p>
  
  <pre><code>    //local variables
      StringTokenizer tokenLine;      //store tokenized line
  
      /******************* Start lineToken Method ***************/
  
      tokenLine = new StringTokenizer(line);  //tokenize the current line of text
  
      return tokenLine;
  
  }//end lineToken
  </code></pre>
  
  <p>public static String flipWord(StringTokenizer tokenLine)
      {
          //local constants</p>
  
  <pre><code>    //local variables
      String word;        //store word for manipulation
      String revWord = "";        //store characters as they are flipped
  
      /******************************* Start flipWord Method******************/
      //store the next token as a string
      word = tokenLine.nextToken();
  
      //for each character store that character to create a new word
      for (int count = word.length(); count &gt; 0; count--)     
          revWord = revWord + word.charAt(count - 1); //store the new word character by character
  
      return revWord;     //return the word reversed
  
  }//end flipWord
  </code></pre>
  
  <p>public static String marginCheck(String revWord) throws Exception
      {
          //local constants
          final int MARGIN = 60;  //maximum characters per line</p>
  
  <pre><code>    //local variables
      String newLine = "";            //store the new line
      FileWriter fw;      //writes to output file
      BufferedWriter bWriter;     //instantiate buffered writer object
      PrintWriter pw;     //instantiate print writer object
      String outFile  =  "RevWord.text";  //file to write to
  
      /************* Start marginCheck Method ************/
      //open the output file for writing  
      fw = new FileWriter(outFile);
      bWriter = new BufferedWriter(fw);
      pw = new PrintWriter(bWriter);
  
      //if the buffered line concatenated with the word is less than the margins
      if (newLine.length() + revWord.length() &lt;= MARGIN)
          newLine = newLine + revWord + " ";      //the buffered line adds the word
      else
          //put an enline character at the end and write the line
          newLine = newLine + "\n";
          pw.println(newLine);
  
          //use this word as the first word of the next line
          newLine = revWord + " ";
      return newLine;     //return for use with flush
  }//end marginCheck
  </code></pre>
  
  <p>public static void flushClose(String inLine) throws Exception
      {
          //local constants</p>
  
  <pre><code>    //local variables
      FileWriter fw;      //writes to output file
      BufferedWriter bWriter;     //instantiate buffered writer object
      String outFile  =  "RevWord.text";  //file to write to
  
      /************ Start flushClose Method *********/
      fw = new FileWriter(outFile);
      bWriter = new BufferedWriter(fw);   //initialize writer object
  
      //write the last line to the output file then flush and close the buffer
      bWriter.write (inLine);
      bWriter.flush();
      bWriter.close();
  }//end flushClose
  </code></pre>