scg/ch14/core/LongFieldReaderTest

From FANG

Jump to: navigation, search

01 package scg.ch14.core;
02 
03 import java.io.File;
04 import java.io.FileNotFoundException;
05 import java.io.FileReader;
06 import java.io.FilterReader;
07 import java.util.Scanner;
08 
09 import scg.ch14.util.ReadAndWrite;
10 
11 import scg.ch14.io.SansCommentFilterReader;
12 
13 /**
14  * A program which reads its command-line arguments, treating each as
15  * the name of a file. A {@link LongFieldReaderTest} object reads its file
16  * one line at a time. Demonstrates how to use {@link
17  * SansCommentFilterReader} (or any other {@link FilterReader}) with a
18  {@link Scanner}.
19  */
20 public class LongFieldReaderTest {
21   File file;
22   public LongFieldReaderTest(String fname{
23     file = new File(fname);
24   }
25 
26   /**
27    * Echo the file, without comments, to standard output.
28    */
29   public void echo() {
30     if (file.exists() && file.canRead()) {
31       try {
32         Scanner echoScanner = 
33           new Scanner(new SansCommentFilterReader(new FileReader(file)));
34         String line;
35         while (echoScanner.hasNext()) {
36           String word = echoScanner.next();
37           if (!(word.equals("BEGIN_LOCATION"|| word.equals("END_LOCATION"))) {
38             String eq = echoScanner.next();
39             String something = ReadAndWrite.readValue(echoScanner);
40             System.out.println("|" + word + "| = |" + something + "|");
41           }
42         }
43         echoScanner.close();
44       } catch (FileNotFoundException e{
45         System.err.println("PANIC: This should never happen!");
46         e.printStackTrace();
47       }
48     } else {
49       System.err.println("Unable to open \"" + file.getName() +
50         "\" for input");
51     }
52   }
53 
54   /**
55    * The arguments are assumed to be the names of files. Each is opened
56    * and echoed to the screen.
57    *
58    @param  args  names of the files to echo
59    */
60   public static void main(String[] args{
61     for (int argNdx = 0; argNdx != args.length; ++argNdx{
62       LongFieldReaderTest nextFile = new LongFieldReaderTest(args[argNdx]);
63       nextFile.echo();
64     }
65   }
66 }
67 
68 //Uploaded on Mon Mar 29 21:39:16 EDT 2010


Download/View scg/ch14/core/LongFieldReaderTest.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