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
05 /**
06 * A program that reads its command-line arguments, treating each as
07 * the name of a file. Create an ExistsFile object with the name of the
08 * file. Then check ifif executes the next statement only if the condition in parenthesis evaluates to true the file exists.
09 */
10 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 ExistsFile {open braces start code blocks and must be matched with a close brace
11 /** Java reference attached to file on file system */
12 File file;
13
14 /**
15 * Create a newnew is used to create objects by calling the constructor ExistsFile with the file pointed to the named file.
16 *
17 * @paramthis is the Javadoc tag for documenting the purpose of parameters fname the name of the file to connect to; the named file
18 * need not exist (it will be checked in exists()).
19 */
20 publicpublic is used to indicate unrestricted access (any other class can have access) ExistsFile(String fname) {open braces start code blocks and must be matched with a close brace
21 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);
22 }close braces end code blocks and must match an earlier open brace
23
24 /**
25 * The arguments are assumed to be the names of files. Each is opened
26 * and echoed to the screen.
27 *
28 * @paramthis is the Javadoc tag for documenting the purpose of parameters args command-line arguments; should name files to be
29 * checked forfor is a looping structure for repeatedly executing a block of code existence
30 */
31 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
32 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
33 ExistsFile nextFile =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ExistsFile(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);
34 nextFile.exists();
35 }close braces end code blocks and must match an earlier open brace
36 }close braces end code blocks and must match an earlier open brace
37
38 /**
39 * Check ifif executes the next statement only if the condition in parenthesis evaluates to true the file exists, printing an appropriate message.
40 */
41 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value exists() {open braces start code blocks and must be matched with a close brace
42 ifif executes the next statement only if the condition in parenthesis evaluates to true (file.exists()) {open braces start code blocks and must be matched with a close brace
43 System.out.println("\"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together "\" is a file.");
44 }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
45 System.out.println("\"" +adds two numbers together or concatenates Strings together file.getName() +adds two numbers together or concatenates Strings together "\" is NOT a file.");
46 }close braces end code blocks and must match an earlier open brace
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
49
50 //Uploaded on Mon Mar 29 21:40:39 EDT 2010
|
Download/View scg/ch10/ExistsFile.java