title: Reversing an entire paragraph of text instead of reversing each consecutive line
body: <p>I was trying to read a text file using FileInputStream and wanted to display all of the words in reverse (so the last word will be the first word etc.) I was able to display all the letters in reverse using a string tokenizer, but not the words.. I've tried a few different methods now and just couldn't seem to do this</p>
  
  <p>Using an ArrayList I could reverse the lines (print the last line first and the first line last).. but I couldn't find a way to reverse all the words</p>
  
  <p>here is what is in my "test.txt" file:</p>
  
  <blockquote>
    <p>One characteristic of Java is portability, which means that computer programs written in the Java language must run similarly on
    any 
    hardware/operating-system platform. This is achieved by compiling the Java language code to an intermediate representation called Java 
    bytecode, instead of directly to platform-specific machine code. Java bytecode instructions are analogous to machine code, but they are
    intended to be interpreted 
    by a virtual machine (VM) written specifically for the host hardware. End-users commonly use a Java Runtime Environment (JRE)
    installed on their own machine for standalone Java applications, or in a Web browser for Java applets.</p>
  </blockquote>
  
  <p>code:</p>
  
  <pre><code>import java.io.*;  
  import java.util.ArrayList;
  
  
  public class RevWords6
  {
      public static void main(String[] args) throws IOException
      {   
          BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt"), "UTF-8"));                
  
          String lines = br.readLine();
          ArrayList&lt;String&gt; buffer = new ArrayList&lt;String&gt;();
  
          //while the data is not null
          while(lines != null)
          {   
              //String words[] = lines.split(" ");
  
              //track formatting
              if(lines != null)
              {   
                  buffer.add("\n");
                  buffer.add(lines);  //I wanted to put the words array in here instead of "lines" but didn't know how
              }
  
              lines=br.readLine();
          }
  
          //reads lines backwards from ArrayList buffer but wanted to read words from end-to-beginning
          for(int i = buffer.size()-1; i&gt;=0; i--)
          {
              System.out.print(buffer.get(i));
          }
  
          br.close();
      }
  }
  </code></pre>
  
  <p>output:</p>
  
  <blockquote>
    <p>installed on their own machine for standalone Java applications, or in
    a Web browser for Java applets. by a virtual machine (VM) written
    specifically for the host hardware. End-users commonly use a Java
    Runtime Environment (JRE) bytecode, instead of directly to
    platform-specific machine code. Java bytecode instructions are
    analogous to machine code, but they are intended to be interpreted 
    hardware/operating-system platform. This is achieved by compiling the
    Java language code to an intermediate representation called Java  One
    characteristic of Java is portability, which means that computer
    programs written in the Java language must run similarly on any</p>
  </blockquote>
  
  <p>As you can see it is printing the lines beginning from bottom to top, but I wanted the words beginning from bottom to top</p>
  
  <p>and here I've tried to use a StringBuffer with a string array splitting the lines by one whitespace, but in this case I could only reverse each line and not reverse the entire paragraph</p>
  
  <p>code:</p>
  
  <pre><code>import java.io.*;  
  
  
  public class RevWords4
  {   
  
      public static void main(String[] args) throws IOException
      {
          BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Conor\\Documents\\test.txt"), "UTF-8"));               
          StringBuffer buffer = new StringBuffer();   
  
          String line = br.readLine();
          String[] word = line.split(" ");
  
          while(line != null)
          {           
              if(line != null)
              {   
                  word = line.split(" ");
  
                  for(int i = word.length-1; i&gt;=0; i--)
                  {
                      StringBuilder sb = new StringBuilder(word[i] + " ");        
                      buffer.append(sb);          
                  }
  
                  buffer.append("\n");
              }
  
              line=br.readLine();
          }
  
  
          System.out.print(buffer);
          br.close();
      }
  }
  </code></pre>
  
  <p>output:</p>
  
  <blockquote>
    <p>any on similarly run must language Java the in written programs
    computer that means which portability, is Java of characteristic One 
    Java called representation intermediate an to code language Java the
    compiling by achieved is This platform. hardware/operating-system 
    interpreted be to intended are they but code, machine to analogous are
    instructions bytecode Java code. machine platform-specific to directly
    of instead bytecode,  (JRE) Environment Runtime Java a use commonly
    End-users hardware. host the for specifically written (VM) machine
    virtual a by  applets. Java for browser Web a in or applications, Java
    standalone for machine own their on installed</p>
  </blockquote>
  
  <p>So I would happily accept any suggestions and tips on this, thank you</p>