scg/ch09/TextFileByWord

From FANG

Jump to: navigation, search

001 package scg.ch09;
002 
003 import java.io.File;
004 import java.io.FileNotFoundException;
005 import java.util.ArrayList;
006 import java.util.Scanner;
007 
008 /**
009  * Non-FANG program: A TextFileByWord object reads a file into a list.
010  * The listLoadFromFile(fname) method reads the contents of the file
011  * word by word, storing the results in the strings ArrayList.
012  *
013  @author  blad
014  */
015 public class TextFileByWord {
016   /**
017    * collection containing all of the strings (words) read from the file
018    */
019   ArrayList<String> strings;
020 
021   /**
022    * Construct a TextFileByWord object; initialize the strings list.
023    */
024   public TextFileByWord() {
025     strings = new ArrayList<String>();
026   }
027 
028   /**
029    * Create a new strings list and fill it from the file. If there are
030    * any problems, the contents of strings is left unchanged.
031    *
032    @param  fname
033    */
034   public void listLoadFromFile(String fname{
035     File file = new File(fname);
036     if (file.exists() && file.canRead()) {
037       try {
038         ArrayList<String> localStrings = new ArrayList<String>();
039         Scanner scanner = new Scanner(file);
040         String word;
041         while (scanner.hasNext()) {
042           word = scanner.next();
043           localStrings.add(word);
044         }
045         strings = localStrings;
046         scanner.close();
047       } catch (FileNotFoundException e{
048         System.out.println("PANIC: This should never happen!");
049         e.printStackTrace();
050       }
051     } else {
052       System.out.println("Unable to open \"" + fname + "\" for input");
053     }
054   }
055 
056   /**
057    * Print out the contents of the strings list, one entry per line.
058    */
059   public void listPrint() {
060     for (int = 0; i != strings.size()++i{
061       System.out.println(strings.get(i));
062     }
063   }
064 
065   /**
066    * Get the number of words stored.
067    *
068    @return  the number of words in strings
069    */
070   public int listSize() {
071     return strings.size();
072   }
073 
074   /**
075    * The argument list should consist of exactly one file name. Thus the
076    * length is checked and if it is right then a TextFileByWord object
077    * is created and used to read and print out information about the
078    * contents of the named file.
079    *
080    @param  args  the command-line arguments; should have one file name
081    */
082   public static void main(String[] args{
083     if (args.length == 1{
084       String fname = args[0];
085       TextFileByWord byWord = new TextFileByWord();
086       byWord.listLoadFromFile(fname);
087       System.out.println("----- listPrint -----");
088       byWord.listPrint();
089       System.out.println("----- listPrint -----");
090       System.out.println("There were " + byWord.listSize() +
091         " words in " + fname);
092     } else {
093       System.out.println(
094         "Usage: provide exactly one (1) file name on command-line.");
095       System.out.println("Program Terminating");
096     }
097   }
098 }
099 
100 //Uploaded on Mon Mar 29 21:41:21 EDT 2010


Download/View scg/ch09/TextFileByWord.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games