From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch10;
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 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
13 /**
14 * internal representation of a file on disk; represents the file
15 * entity, not the contents
16 */
17 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) File file;
18
19 /**
20 * 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 EchoFile}close braces end code blocks and must match an earlier open brace object with the given file name.
21 *
22 * @paramnull fname full path to the file to echo
23 */
24 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
25 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);
26 }close braces end code blocks and must match an earlier open brace
27
28 /**
29 * The arguments are assumed to be the names of files. Each is opened
30 * and echoed to the screen.
31 *
32 * @paramnull args
33 */
34 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
35 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
36 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);
37 nextFile.echo();
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
40
41 /**
42 * Echo the file to standard output. If there is a problem with the
43 * file not existing or not being readable by the current user, an
44 * appropriate error message should be printed.
45 */
46 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
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 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
49 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);
50 String line;
51 whilewhile is a looping structure for executing code repeatedly (echoScanner.hasNextLine()) {open braces start code blocks and must be matched with a close brace
52 line =this assignment operator makes the left side equal to the right side echoScanner.nextLine();
53 System.out.println(line);
54 }close braces end code blocks and must match an earlier open brace
55 echoScanner.close();
56 }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
57 System.out.println("PANIC: This should never happen!");
58 e.printStackTrace();
59 }close braces end code blocks and must match an earlier open brace
60 }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
61 System.out.println("Unable to open \"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together
62 "\" for input");
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
65 }close braces end code blocks and must match an earlier open brace
66
67 //Uploaded on Mon Mar 29 21:41:01 EDT 2010
|
Download/View scg/ch10/EchoFile.java