title: More efficient or more modern? Reading in & Sorting A Text File With Java
body: <p>I've been trying to upgrade my Java skills to use more of Java 5 &amp; Java 6.   I've been playing around with some programming exercises.   I was asked to read in a paragraph from a text file and output a sorted (descending) list of words and output the count of each word.</p>
  
  <p>My code is below.</p>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p>Is my file input routine the most respectful of JVM resources?</p></li>
  <li><p>Is it possible to cut steps out in regards to reading the file contents and getting the content into a collection that can make a sorted list of words?</p></li>
  <li><p>Am I using the Collection classes and interface the most efficient way I can?</p></li>
  </ol>
  
  <p>Thanks much for any opinions.  I'm just trying to have some fun and improve my programming skills.</p>
  
  <pre><code>import java.io.*;
  import  java.util.*;
  
  public class Sort
  {
      public static void main(String[] args)
      {
          String   sUnsorted       = null;
          String[] saSplit         = null;
  
          int iCurrentWordCount    = 1;
          String currentword       = null;
          String pastword          = "";
  
          // Read the text file into a string
          sUnsorted = readIn("input1.txt");
  
          // Parse the String by white space into String array of single words
          saSplit   = sUnsorted.split("\\s+");
  
          // Sort the String array in descending order
          java.util.Arrays.sort(saSplit, Collections.reverseOrder());
  
  
          // Count the occurences of each word in the String array
          for (int i = 0; i &lt; saSplit.length; i++ )
          {
  
              currentword = saSplit[i];
  
              // If this word was seen before, increase the count &amp; print the
              // word to stdout
              if ( currentword.equals(pastword) )
              {
                  iCurrentWordCount ++;
                  System.out.println(currentword);
              }
              // Output the count of the LAST word to stdout,
              // Reset our counter
              else if (!currentword.equals(pastword))
              {
  
                  if ( !pastword.equals("") )
                  {
  
                      System.out.println("Word Count for " + pastword + ": " + iCurrentWordCount);
  
                  }
  
  
                  System.out.println(currentword );
                  iCurrentWordCount = 1;
  
              }
  
              pastword = currentword;  
          }// end for loop
  
         // Print out the count for the last word processed
         System.out.println("Word Count for " + currentword + ": " + iCurrentWordCount);
  
  
  
      }// end funciton main()
  
  
      // Read The Input File Into A String      
      public static String readIn(String infile)
      {
          String result = " ";
  
          try
          {
              FileInputStream file = new FileInputStream (infile);
              DataInputStream in   = new DataInputStream (file);
              byte[] b             = new byte[ in.available() ];
  
              in.readFully (b);
              in.close ();
  
              result = new String (b, 0, b.length, "US-ASCII");
  
          }
          catch ( Exception e )
          {
              e.printStackTrace();
          }
  
          return result;
      }// end funciton readIn()
  
  }// end class Sort()
  
  /////////////////////////////////////////////////
  //  Updated Copy 1, Based On The Useful Comments
  //////////////////////////////////////////////////
  
  import java.io.*;
  import java.util.*;
  
  public class Sort2
  {
      public static void main(String[] args) throws Exception
      {
          // Scanner will tokenize on white space, like we need
          Scanner scanner               = new Scanner(new FileInputStream("input1.txt"));
          ArrayList &lt;String&gt; wordlist   = new  ArrayList&lt;String&gt;();
          String currentword            = null;   
          String pastword               = null;
          int iCurrentWordCount         = 1;       
  
          while (scanner.hasNext())
              wordlist.add(scanner.next() );
  
          // Sort in descending natural order
          Collections.sort(wordlist);
          Collections.reverse(wordlist);
  
          for ( String temp : wordlist )
          {
              currentword = temp;
  
              // If this word was seen before, increase the count &amp; print the
              // word to stdout
              if ( currentword.equals(pastword) )
              {
                  iCurrentWordCount ++;
                  System.out.println(currentword);
              }
              // Output the count of the LAST word to stdout,
              // Reset our counter
              else //if (!currentword.equals(pastword))
              {
                  if ( pastword != null )
                      System.out.println("Count for " + pastword + ": " +  
                                                              CurrentWordCount);   
  
                  System.out.println(currentword );
                  iCurrentWordCount = 1;    
              }
  
              pastword = currentword;  
          }// end for loop
  
          System.out.println("Count for " + currentword + ": " + iCurrentWordCount);
  
      }// end funciton main()
  
  
  }// end class Sort2
  </code></pre>