Sunday, May 20, 2007

Searching Impl.

This is one bad implementation of the last blog. Need to change a lot of part to run in generic way, I am too lazy :)


import java.util.*;
public class SearchFile
{
public static void main(String args[])
{
try
{
String osName = System.getProperty("os.name" );
System.out.println(osName);
String[] cmd = new String[5];
if( osName.equals( "Windows Vista" ) )
{
cmd[0] = "cmd.exe" ;
cmd[1] = "/C" ;
cmd[2] = "dir *.java /s";
cmd[3] = ">";
cmd[4] = "Hello.txt";
}
/* else if( osName.equals( "Unix" ) )
{
find name filename

...

} */
Runtime rt = Runtime.getRuntime();
System.out.println("Executing " + cmd[0] + " " + cmd[1]
+ " " + cmd[2]+ " " + cmd[3]+ " " + cmd[4]);
Process proc = rt.exec(cmd);
int exitVal = proc.waitFor();
System.out.println("ExitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}

Look at the search speed after first run. Its simple awesome. But it will create problem if user dont have permission to run a process.

2 comments:

Bipin "3~" Upadhyay said...

That's a pretty interesting code to take people for sneak-peek to the System and Runtime Classes.

Additionally, this code may be modified to take filename/pattern as input. However, I really don't find any real need for this code, apart from the fact that it can be done.
Wait a minute!
That's the reason I always need to do something. :)

Vaibhav Choudhary said...

Ha ha .. its my timepass as well. Actually I was looking for a code where given on dir name and file name , java returns me the path (dir and sub-dir both). Unfortunatly there is no API in java. So, I decided to write a code. Recursivly searching in all dir and sub-dir. I thought why not use the search already made by great ppl thats it ! this code has lot of limitation.