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 importimport means to make the classes and/or packages available in this program java.io.Reader;
08 importimport means to make the classes and/or packages available in this program java.util.Scanner;
09
10 importimport means to make the classes and/or packages available in this program scg.ch14.io.NoCurlyFilterReader;
11
12 /**
13 * A program which reads its command-line arguments, treating each as
14 * the name of a file. An EchoFile object reads its file one line at a
15 * time and echos them to standard output.
16 */
17 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 EchoFileWithFilterReader {open braces start code blocks and must be matched with a close brace
18 /**
19 * internal representation of a file on disk; represents the file
20 * entity, not the contents
21 */
22 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) File file;
23
24 /**
25 * 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 EchoFileWithFilterReader}close braces end code blocks and must match an earlier open brace object with the
26 * given file name.
27 *
28 * @paramnull fname full path to the file to echo
29 */
30 publicpublic is used to indicate unrestricted access (any other class can have access) EchoFileWithFilterReader(String fname) {open braces start code blocks and must be matched with a close brace
31 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);
32 }close braces end code blocks and must match an earlier open brace
33
34 /**
35 * The arguments are assumed to be the names of files. Each is opened
36 * and echoed to the screen.
37 *
38 * @paramnull args
39 */
40 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
41 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
42 EchoFileWithFilterReader nextFile =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor EchoFileWithFilterReader(
43 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);
44 nextFile.echo();
45 }close braces end code blocks and must match an earlier open brace
46 }close braces end code blocks and must match an earlier open brace
47
48 /**
49 * Echo the file to standard output. If there is a problem with the
50 * file not existing or not being readable by the current user, an
51 * appropriate error message should be printed.
52 */
53 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value echo() {open braces start code blocks and must be matched with a close brace
54 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
55 Reader 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;
56 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
57 reader =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor NoCurlyFilterReader(newnew is used to create objects by calling the constructor FileReader(file));
58 Scanner echoScanner =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(reader);
59 String line;
60 whilewhile is a looping structure for executing code repeatedly (echoScanner.hasNextLine()) {open braces start code blocks and must be matched with a close brace
61 line =this assignment operator makes the left side equal to the right side echoScanner.nextLine();
62 System.out.println(line);
63 }close braces end code blocks and must match an earlier open brace
64 }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
65 System.out.println("PANIC: This should never happen!");
66 e.printStackTrace();
67 }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
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 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
70 reader.close();
71 }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
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 e.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
79 }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
80 System.out.println("Unable to open \"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together
81 "\" for input");
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 }close braces end code blocks and must match an earlier open brace
85
86 //Uploaded on Mon Mar 29 21:39:37 EDT 2010
|
Download/View scg/ch14/core/EchoFileWithFilterReader.java