Thursday, July 7, 2011

Java Code to execute DOS Commands

/*
First set the path of JDK and execute the program
you will get the output in the MYOUTPUT.txt file

*/

import java.io.*;

public class ExecDosCmd
{

public static void main(String rk[])
{
Process p = null;
Runtime rt;
String line = null;
int i=0;

try
{
FileOutputStream file=new

FileOutputStream("MYOUTPUT.txt");
rt = Runtime.getRuntime();
p=rt.exec("cmd /c dir");
BufferedReader br = new BufferedReader (new

InputStreamReader(p.getInputStream()));
while((i=br.read())!=-1)
{
file.write((char)i);


}

} catch(Exception e)
{
System.out.println(e);
}
}
}

No comments:

Post a Comment