Saturday, January 11, 2014

A Low Performance Search program in Java

import java.io.*;
import java.util.regex.*;
class FindString3
{
public static void main(String args[])
{


try
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String t;
System.out.println("Enter The text to serach : ");
t=br.readLine();

File f=new File("D:\\");
Extract(f,t);
}catch(Exception e)
{
System.out.println("Error:" + e);
}
}

public static void Extract(File f1,String text)
{
try
{
File f=f1;
File[] list=f.listFiles();
FileInputStream fi;
DataInputStream d;
String s;
Pattern pt;
Matcher mc;
int count=0 ; 
for(int j=0 ; j < list.length; ++j)
{
if(list[j].isFile() && list[j].canRead())
{
fi=new FileInputStream(list[j]);
d=new DataInputStream(fi);

while((s=d.readLine()) != null)
{
pt=Pattern.compile(text);
mc=pt.matcher(s);
if(mc.find())
{
++count;
break;
}
}
if(count > 0 )
{
System.out.println(list[j].getPath() + list[j].getName()); 
count=0;
}
}
if(list[j].isDirectory())
Extract(list[j],text);
}

}catch(Exception e1)
{
System.out.println("Error:" + e1);
System.exit(0);
}
}
}

Post a Comment