scg/ch10/TextFileByWord

From FANG

Jump to: navigation, search

01 package scg.ch10;
02 
03 import java.io.File;
04 import java.io.FileNotFoundException;
05 import java.util.ArrayList;
06 import java.util.Scanner;
07 
08 /**
09  * Non-FANG program: A TextFileByWord object reads a file into a list.
10  * The listLoadFromFile(fname) method reads the contents of the file
11  * word by word, storing the results in the strings ArrayList.
12  */
13 public class TextFileByWord {
14   /**
15    * collection containing all of the strings (words) read from the file
16    */
17   ArrayList<String> strings;
18 
19   /**
20    * Construct a TextFileByWord object; initialize the strings list.
21    */
22   public TextFileByWord() {
23     strings = new ArrayList<String>();
24   }
25 
26   /**
27    * The argument list should consist of exactly one file name. Thus the
28    * length is checked and if it is right then a TextFileByWord object
29    * is created and used to read and print out information about the
30    * contents of the named file.
31    *
32    @param  args  the command-line arguments; should have one file name
33    */
34   public static void main(String[] args{
35     if (args.length == 1{
36       String fname = args[0];
37       TextFileByWord byWord = new TextFileByWord();
38       byWord.listLoadFromFile(fname);
39       System.out.println("----- listPrint -----");
40       byWord.listPrint();
41       System.out.println("----- listPrint -----");
42       System.out.println("There were " + byWord.listSize() +
43         " words in " + fname);
44     } else {
45       System.out.println(
46         "Usage: provide exactly one (1) file name on command-line.");
47       System.out.println("Program Terminating");
48     }
49   }
50 
51   /**
52    * Create a new strings list and fill it from the file. If there are
53    * any problems, the contents of strings is left unchanged.
54    *
55    @param  fname
56    */
57   public void listLoadFromFile(String fname{
58     File file = new File(fname);
59     if (file.exists() && file.canRead()) {
60       try {
61         ArrayList<String> localStrings = new ArrayList<String>();
62         Scanner scanner = new Scanner(file);
63         String word;
64         while (scanner.hasNext()) {
65           word = scanner.next();
66           localStrings.add(word);
67         }
68         strings = localStrings;
69         scanner.close();
70       } catch (FileNotFoundException e{
71         System.out.println("PANIC: This should never happen!");
72         e.printStackTrace();
73       }
74     } else {
75       System.out.println("Unable to open \"" + fname + "\" for input");
76     }
77   }
78 
79   /**
80    * Print out the contents of the strings list, one entry per line.
81    */
82   public void listPrint() {
83     for (int = 0; i != strings.size()++i{
84       System.out.println(strings.get(i));
85     }
86   }
87 
88   /**
89    * Get the number of words stored.
90    *
91    @return  the number of words in strings
92    */
93   public int listSize() {
94     return strings.size();
95   }
96 }
97 
98 //Uploaded on Mon Mar 29 21:39:48 EDT 2010


Download/View scg/ch10/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