scg/ch10/EchoFileFANG

From FANG

Jump to: navigation, search

01 package scg.ch10;
02 
03 import java.io.File;
04 import java.io.FileNotFoundException;
05 import java.util.Scanner;
06 
07 import fang2.core.Game;
08 import fang2.sprites.InputStringField;
09 
10 /**
11  * A program which reads its command-line arguments, treating each as
12  * the name of a file. An EchoFile object reads its file one line at a
13  * time and echos them to standard output.
14  */
15 public class EchoFileFANG
16   extends Game {
17   private File file;
18   InputStringField iss;
19   @Override
20   public void advance(double dT{
21   }
22 
23   /**
24    * Echo the file to standard output. If there is a problem with the
25    * file not existing or not being readable by the current user, an
26    * appropriate error message should be printed.
27    */
28   public void echo(File file{
29     if (file.exists() && file.canRead()) {
30       try {
31         Scanner echoScanner = new Scanner(file);
32         String line;
33         while (echoScanner.hasNextLine()) {
34           line = echoScanner.nextLine();
35           System.out.println(line);
36         }
37         echoScanner.close();
38       } catch (FileNotFoundException e{
39         System.out.println("PANIC: This should never happen!");
40         e.printStackTrace();
41       }
42     } else {
43       System.out.println("Unable to open \"" + file.getName() +
44         "\" for input");
45     }
46   }
47 
48   @Override
49   public void setup() {
50   }
51 }
52 
53 //Uploaded on Mon Mar 29 21:38:37 EDT 2010


Download/View scg/ch10/EchoFileFANG.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