scg/ch09/EchoFile

From FANG

Jump to: navigation, search

01 package scg.ch09;
02 
03 import java.io.File;
04 import java.io.FileNotFoundException;
05 import 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  @author  blad
13  */
14 public class EchoFile {
15   File file;
16   public EchoFile(String fname{
17     file = new File(fname);
18   }
19 
20   /**
21    * Echo the file to standard output. If there is a problem with the
22    * file not existing or not being readable by the current user, an
23    * appropriate error message should be printed.
24    */
25   public void echo() {
26     if (file.exists() && file.canRead()) {
27       try {
28         Scanner echoScanner = new Scanner(file);
29         String line;
30         while (echoScanner.hasNextLine()) {
31           line = echoScanner.nextLine();
32           System.out.println(line);
33         }
34         echoScanner.close();
35       } catch (FileNotFoundException e{
36         System.out.println("PANIC: This should never happen!");
37         e.printStackTrace();
38       }
39     } else {
40       System.out.println("Unable to open \"" + file.getName() +
41         "\" for input");
42     }
43   }
44 
45   /**
46    * The arguments are assumed to be the names of files. Each is opened
47    * and echoed to the screen.
48    *
49    @param  args
50    */
51   public static void main(String[] args{
52     for (int argNdx = 0; argNdx != args.length; ++argNdx{
53       EchoFile nextFile = new EchoFile(args[argNdx]);
54       nextFile.echo();
55     }
56   }
57 }
58 
59 //Uploaded on Mon Mar 29 21:42:22 EDT 2010


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