From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch09;
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.util.Scanner;
06
07 /**
08 * A program which reads its command-line arguments, treating each as
09 * the name of a file. An EchoFile object reads its file one line at a
10 * time and echos them to standard output.
11 *
12 * @authorthis is the Javadoc tag for documenting who created the source code blad
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 EchoFile {open braces start code blocks and must be matched with a close brace
15 File file;
16 publicpublic is used to indicate unrestricted access (any other class can have access) EchoFile(String fname) {open braces start code blocks and must be matched with a close brace
17 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);
18 }close braces end code blocks and must match an earlier open brace
19
20 /**
21 * Echo the file to standard output. If there is a problem with the
22 * file not existing or not being readable by the current user, an
23 * appropriate error message should be printed.
24 */
25 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
26 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
27 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
28 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(file);
29 String line;
30 whilewhile is a looping structure for executing code repeatedly (echoScanner.hasNextLine()) {open braces start code blocks and must be matched with a close brace
31 line =this assignment operator makes the left side equal to the right side echoScanner.nextLine();
32 System.out.println(line);
33 }close braces end code blocks and must match an earlier open brace
34 echoScanner.close();
35 }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
36 System.out.println("PANIC: This should never happen!");
37 e.printStackTrace();
38 }close braces end code blocks and must match an earlier open brace
39 }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
40 System.out.println("Unable to open \"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together
41 "\" for input");
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 * The arguments are assumed to be the names of files. Each is opened
47 * and echoed to the screen.
48 *
49 * @paramthis is the Javadoc tag for documenting the purpose of parameters args
50 */
51 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
52 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
53 EchoFile nextFile =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor EchoFile(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);
54 nextFile.echo();
55 }close braces end code blocks and must match an earlier open brace
56 }close braces end code blocks and must match an earlier open brace
57 }close braces end code blocks and must match an earlier open brace
58
59 //Uploaded on Mon Mar 29 21:42:22 EDT 2010
|
Download/View scg/ch09/EchoFile.java