Pattern Matching implementation in JAVA implementing Directory
import java.io.*;
class Mat
{
public String patt;
public int flg=0;
public Mat(String p)
{
patt=p;
}
public void chk(File f) throws IOException
{
if (f.isDirectory())
{
File[] lf = f.listFiles();
for (int i = 0; i < lf.length; i++)
{
chk(lf[i]);
}
}
else
srch(f);
}
public void srch(File f) throws IOException
{
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
BufferedReader brd = new BufferedReader(new InputStreamReader(in));
String strLine;
int l=0;
while ((strLine = brd.readLine()) != null)
{
l=l+1;
int n=strLine.length();
int m=patt.length();
char p;
char t;
int s=0;
int i=0;
int j=0;
while(i<n && s!=1)
{
t=strLine.charAt(i);
p=patt.charAt(j);
if(p==t)
{
i++;
j++;
if(j>=m)
{
s=1;
}
}
else
{
j=0;
i++;
}
}
if(s==1)
{
System.out.println("Found Pattern at Line "+ l+" in file" +f);
flg=flg+1;
}
s=0;
}
}
}
class Pat
{
public static void main(String[] args) throws IOException
{
String patt;
File f=new File("D:\\Sameer\\Pat.java");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("enter pattern");
patt=br.readLine();
File f1=f.getParentFile();
Mat m=new Mat(patt);
m.chk(f1);
System.out.println("Pattern Found "+m.flg+" times");
}
}
import java.io.*;
class Mat
{
public String patt;
public int flg=0;
public Mat(String p)
{
patt=p;
}
public void chk(File f) throws IOException
{
if (f.isDirectory())
{
File[] lf = f.listFiles();
for (int i = 0; i < lf.length; i++)
{
chk(lf[i]);
}
}
else
srch(f);
}
public void srch(File f) throws IOException
{
FileInputStream fstream = new FileInputStream(f);
DataInputStream in = new DataInputStream(fstream);
BufferedReader brd = new BufferedReader(new InputStreamReader(in));
String strLine;
int l=0;
while ((strLine = brd.readLine()) != null)
{
l=l+1;
int n=strLine.length();
int m=patt.length();
char p;
char t;
int s=0;
int i=0;
int j=0;
while(i<n && s!=1)
{
t=strLine.charAt(i);
p=patt.charAt(j);
if(p==t)
{
i++;
j++;
if(j>=m)
{
s=1;
}
}
else
{
j=0;
i++;
}
}
if(s==1)
{
System.out.println("Found Pattern at Line "+ l+" in file" +f);
flg=flg+1;
}
s=0;
}
}
}
class Pat
{
public static void main(String[] args) throws IOException
{
String patt;
File f=new File("D:\\Sameer\\Pat.java");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
System.out.println("enter pattern");
patt=br.readLine();
File f1=f.getParentFile();
Mat m=new Mat(patt);
m.chk(f1);
System.out.println("Pattern Found "+m.flg+" times");
}
}