scg/ch09/ExistsFile

From FANG

Jump to: navigation, search

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


Download/View scg/ch09/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