Monday, October 22, 2012

Search Pattern in Java

Program to find a specific pattern from a given text or file is stated as below.



import java.io.*;

class srchPattern
{
  public static void main(String args[])
  {
    if(args.length == 0 || args.length == 1)
    {
      System.out.println("Insufficient arguments...");
      return;
    }

    File f = new File(args[1]);

    try
    {
      BufferedReader i = new BufferedReader(new FileReader(args[1]));

      String line;
      int ct = 0;

      if(f.exists() && f.isFile())
      {
        while((line = i.readLine()) != null)
        {
          if((line.indexOf(args[0])) != -1)
          {
            ct++;
          }
          System.out.println(line + " ");
        }

        if(ct == 0)
        {
          System.out.println("Pattern not Found");
        }
        else
        {
          System.out.println("Pattern found in the file...");
          System.out.println("Number of Occurances of the word " + args[0] + " in te file " + args[1] + " is " + ct);
        }
      }
    }
    catch(Exception e){}
  }
}

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...