10 date 2008.04.03.18.45.33; author rmh3093; state Exp;
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;
47 public class FindString2 {
50 * Seach for the occurences of string in file
52 * @@param args command line arguments
54 public static void main(String[] args) {
56 if ((args.length < 2) || (args.length > 3)) {
57 System.out.println("Usage: java FindString2 search-string " +
58 "infile-name [ outfile-name ]");
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);
67 printwriter = new PrintWriter(args[2]);
69 CharSequence sequence = args[0];
70 String s = buffer.readLine();
72 if (s.contains(sequence)) {
73 printwriter.println(s);
75 s = buffer.readLine();
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());