scg/ch10/ExistsFile

From FANG

Jump to: navigation, search

01 package scg.ch10;
02 
03 import 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 if the file exists.
09  */
10 public class ExistsFile {
11   /** Java reference attached to file on file system */
12   File file;
13 
14   /**
15    * Create a new ExistsFile with the file pointed to the named file.
16    *
17    @param  fname  the name of the file to connect to; the named file
18    *                need not exist (it will be checked in exists()).
19    */
20   public ExistsFile(String fname{
21     file = new File(fname);
22   }
23 
24   /**
25    * The arguments are assumed to be the names of files. Each is opened
26    * and echoed to the screen.
27    *
28    @param  args  command-line arguments; should name files to be
29    *               checked for existence
30    */
31   public static void main(String[] args{
32     for (int argNdx = 0; argNdx != args.length; ++argNdx{
33       ExistsFile nextFile = new ExistsFile(args[argNdx]);
34       nextFile.exists();
35     }
36   }
37 
38   /**
39    * Check if the file exists, printing an appropriate message.
40    */
41   public void exists() {
42     if (file.exists()) {
43       System.out.println("\"" + file.getName() + "\" is a file.");
44     } else {
45       System.out.println("\"" + file.getName() + "\" is NOT a file.");
46     }
47   }
48 }
49 
50 //Uploaded on Mon Mar 29 21:40:39 EDT 2010


Download/View scg/ch10/ExistsFile.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games