From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch14.core;
02
03 importimport means to make the classes and/or packages available in this program java.io.File;
04 importimport means to make the classes and/or packages available in this program java.io.FileNotFoundException;
05 importimport means to make the classes and/or packages available in this program java.io.FileReader;
06 importimport means to make the classes and/or packages available in this program java.io.IOException;
07
08 /**
09 * A program which reads its command-line arguments, treating each as
10 * the name of a file. A {open braces start code blocks and must be matched with a close brace@link CountCharacters}close braces end code blocks and must match an earlier open brace object reads its file
11 * one character at a time using a {open braces start code blocks and must be matched with a close brace@link FileReader}close braces end code blocks and must match an earlier open brace.
12 */
13 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects CountCharacters {open braces start code blocks and must be matched with a close brace
14 /** named constant forfor is a looping structure for repeatedly executing a block of code the end of file returnreturn means to provide the result of the method and/or cease execution of the method immediately value */
15 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer EOF =this assignment operator makes the left side equal to the right side -1;
16
17 /**
18 * The arguments are assumed to be the names of files. Each is opened
19 * and echoed to the screen.
20 *
21 * @paramthis is the Javadoc tag for documenting the purpose of parameters args names of the files to echo
22 */
23 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value mainThe main method is the place where applications begin executing.(String[brackets are typically used to declare, initialize and index (indicate which element of) arrays]brackets are typically used to declare, initialize and index (indicate which element of) arrays args) {open braces start code blocks and must be matched with a close brace
24 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer argNdx =this assignment operator makes the left side equal to the right side 0; argNdx !=this is the not equals operator which evaluates to true if both sides are different args.length; ++this is the increment operator, which increases the variable by 1argNdx) {open braces start code blocks and must be matched with a close brace
25 CountCharacters nextFile =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor CountCharacters(args[brackets are typically used to declare, initialize and index (indicate which element of) arraysargNdx]brackets are typically used to declare, initialize and index (indicate which element of) arrays);
26 nextFile.count();
27 }close braces end code blocks and must match an earlier open brace
28 }close braces end code blocks and must match an earlier open brace
29
30 /** the file to open */
31 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) File file;
32
33 /**
34 * Construct a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link CountCharacters}close braces end code blocks and must match an earlier open brace object forfor is a looping structure for repeatedly executing a block of code the named file.
35 *
36 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname the full path name of the file
37 */
38 publicpublic is used to indicate unrestricted access (any other class can have access) CountCharacters(String fname) {open braces start code blocks and must be matched with a close brace
39 file =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor File(fname);
40 }close braces end code blocks and must match an earlier open brace
41
42 /**
43 * If possible read the file, character-by-character, counting the
44 * characters.
45 */
46 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value count() {open braces start code blocks and must be matched with a close brace
47 ifif executes the next statement only if the condition in parenthesis evaluates to true (file.exists() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) file.canRead()) {open braces start code blocks and must be matched with a close brace
48 FileReader reader =this assignment operator makes the left side equal to the right side nullnull is the value used to refer to a non-existant object;
49 trytry is for executing a code block that may experience exceptions (errors) {open braces start code blocks and must be matched with a close brace
50 reader =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor FileReader(file);
51 intint is the type for whole numbers and it is short for integer characterCount =this assignment operator makes the left side equal to the right side 0;
52
53 intint is the type for whole numbers and it is short for integer ch =this assignment operator makes the left side equal to the right side reader.read();
54 whilewhile is a looping structure for executing code repeatedly (ch !=this is the not equals operator which evaluates to true if both sides are different EOF) {open braces start code blocks and must be matched with a close brace
55 ++this is the increment operator, which increases the variable by 1characterCount;
56 ch =this assignment operator makes the left side equal to the right side reader.read();
57 }close braces end code blocks and must match an earlier open brace
58 System.out.println(file.getName() +adds two numbers together or concatenates Strings together ": " +adds two numbers together or concatenates Strings together characterCount);
59 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (FileNotFoundException e) {open braces start code blocks and must be matched with a close brace
60 System.err.println("PANIC: This should never happen!");
61 e.printStackTrace();
62 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (IOException e) {open braces start code blocks and must be matched with a close brace// file was opened before this exception
63 System.err.println("Problem while reading \"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together
64 "\".");
65 e.printStackTrace();
66 }close braces end code blocks and must match an earlier open brace finallyfinally is what always executes at the end of a try block {open braces start code blocks and must be matched with a close brace
67 trytry is for executing a code block that may experience exceptions (errors) {open braces start code blocks and must be matched with a close brace
68 ifif executes the next statement only if the condition in parenthesis evaluates to true (reader !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
69 reader.close();
70 }close braces end code blocks and must match an earlier open brace
71 }close braces end code blocks and must match an earlier open brace catchcatch means to handle an exception that may occur (IOException e1) {open braces start code blocks and must be matched with a close brace
72 System.err.println(
73 "Error closing Reader assciated with + \"" +adds two numbers together or concatenates Strings together
74 file.getName() +adds two numbers together or concatenates Strings together "\".");
75 e1.printStackTrace();
76 }close braces end code blocks and must match an earlier open brace
77 }close braces end code blocks and must match an earlier open brace
78 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
79 System.err.println("Unable to open \"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together
80 "\" for input");
81 }close braces end code blocks and must match an earlier open brace
82 }close braces end code blocks and must match an earlier open brace
83 }close braces end code blocks and must match an earlier open brace
84
85 //Uploaded on Mon Mar 29 21:40:01 EDT 2010
|
Download/View scg/ch14/core/CountCharacters.java