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