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.FilterReader;
07 importimport means to make the classes and/or packages available in this program java.util.Scanner;
08
09 importimport means to make the classes and/or packages available in this program scg.ch14.util.ReadAndWrite;
10
11 importimport means to make the classes and/or packages available in this program scg.ch14.io.SansCommentFilterReader;
12
13 /**
14 * A program which reads its command-line arguments, treating each as
15 * the name of a file. A {open braces start code blocks and must be matched with a close brace@link LongFieldReaderTest}close braces end code blocks and must match an earlier open brace object reads its file
16 * one line at a time. Demonstrates how to use {open braces start code blocks and must be matched with a close brace@link
17 * SansCommentFilterReader}close braces end code blocks and must match an earlier open brace (or any other {open braces start code blocks and must be matched with a close brace@link FilterReader}close braces end code blocks and must match an earlier open brace) with a
18 * {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace.
19 */
20 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 LongFieldReaderTest {open braces start code blocks and must be matched with a close brace
21 File file;
22 publicpublic is used to indicate unrestricted access (any other class can have access) LongFieldReaderTest(String fname) {open braces start code blocks and must be matched with a close brace
23 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);
24 }close braces end code blocks and must match an earlier open brace
25
26 /**
27 * Echo the file, without comments, to standard output.
28 */
29 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
30 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
31 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
32 Scanner echoScanner =this assignment operator makes the left side equal to the right side
33 newnew is used to create objects by calling the constructor Scanner(newnew is used to create objects by calling the constructor SansCommentFilterReader(newnew is used to create objects by calling the constructor FileReader(file)));
34 String line;
35 whilewhile is a looping structure for executing code repeatedly (echoScanner.hasNext()) {open braces start code blocks and must be matched with a close brace
36 String word =this assignment operator makes the left side equal to the right side echoScanner.next();
37 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to true(word.equals("BEGIN_LOCATION") ||this is boolean or, meaning if either or both are true then the result is true word.equals("END_LOCATION"))) {open braces start code blocks and must be matched with a close brace
38 String eq =this assignment operator makes the left side equal to the right side echoScanner.next();
39 String something =this assignment operator makes the left side equal to the right side ReadAndWrite.readValue(echoScanner);
40 System.out.println("|" +adds two numbers together or concatenates Strings together word +adds two numbers together or concatenates Strings together "| = |" +adds two numbers together or concatenates Strings together something +adds two numbers together or concatenates Strings together "|");
41 }close braces end code blocks and must match an earlier open brace
42 }close braces end code blocks and must match an earlier open brace
43 echoScanner.close();
44 }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
45 System.err.println("PANIC: This should never happen!");
46 e.printStackTrace();
47 }close braces end code blocks and must match an earlier open brace
48 }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
49 System.err.println("Unable to open \"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together
50 "\" for input");
51 }close braces end code blocks and must match an earlier open brace
52 }close braces end code blocks and must match an earlier open brace
53
54 /**
55 * The arguments are assumed to be the names of files. Each is opened
56 * and echoed to the screen.
57 *
58 * @paramnull args names of the files to echo
59 */
60 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
61 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
62 LongFieldReaderTest nextFile =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LongFieldReaderTest(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);
63 nextFile.echo();
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 }close braces end code blocks and must match an earlier open brace
67
68 //Uploaded on Mon Mar 29 21:39:16 EDT 2010
|
Download/View scg/ch14/core/LongFieldReaderTest.java