lajdlksadlmla
[rmh3093.git] / lab4 / RCS / FindString2.java,v
blobdc99005c7eac61c45dbd3cc0626f11ffd1c1f768
1 head    1.1;
2 access;
3 symbols;
4 locks
5         rmh3093:1.1; strict;
6 comment @# @;
9 1.1
10 date    2008.04.03.18.45.33;    author rmh3093; state Exp;
11 branches;
12 next    ;
15 desc
16 @initial commit
20 1.1
21 log
22 @Initial revision
24 text
25 @/*
26  * FindString2.java
27  *
28  * Version:
29  *     $Id$
30  *
31  * Revisions:
32  *     $Log$
33  */
35 import java.io.BufferedReader;
36 import java.io.FileNotFoundException;
37 import java.io.FileOutputStream;
38 import java.io.FileReader;
39 import java.io.IOException;
40 import java.io.OutputStream;
41 import java.io.PrintWriter;
43 /**
44  * @@author rmh3093
45  *
46  */
47 public class FindString2 {
49         /**
50          * Seach for the occurences of string in file
51          * 
52          * @@param args command line arguments
53          */
54         public static void main(String[] args) {
55                 
56                 if ((args.length < 2) || (args.length > 3)) {
57                         System.out.println("Usage: java FindString2 search-string " +
58                                         "infile-name [ outfile-name ]");
59                 } else {
60                         try {
61                                 FileReader filereader = new FileReader(args[1]);
62                                 BufferedReader buffer = new BufferedReader(filereader);
63                                 PrintWriter printwriter = null;
64                                 if (args.length < 3) {
65                                         printwriter = new PrintWriter(System.out);
66                                 } else {
67                                         printwriter = new PrintWriter(args[2]);
68                                 }
69                                 CharSequence sequence = args[0];
70                                 String s = buffer.readLine();
71                                 while (s != null) { 
72                                         if (s.contains(sequence)) {
73                                                 printwriter.println(s);
74                                         }
75                                         s = buffer.readLine();
76                                 }
77                                 buffer.close();
78                                 filereader.close();
79                                 printwriter.close();
80                         } catch (FileNotFoundException e) {
81                                 System.err.printf("%s occurred: %s\n", 
82                                                 e.getClass().getSimpleName(),e.getMessage());
83                         } catch (IOException e) {
84                                 System.err.printf("%s occurred: %s\n", 
85                                                 e.getClass().getSimpleName(),e.getMessage());
86                         }
87                 }
88                 
89         }
90