Redirecting command line output to a text file using java program
807589Jan 2 2009 — edited Jan 2 2009Hi All,
I am trying execute the commands that can be run using cmd using java program and code is as below.
I want to redirect the output to the text file in following code,But i am not getting how to do it.Can anyone suggest me the solution.
Once i execute the following code the command dir gets executed in cmd.
Even i have tried using > operator to redirect but the output was not getting redirected.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
interface IPerlService
{
public String BZMCreateZone(String s);
public int BZMRenew();
}
public class test
{
public void BZMCreateZone()
{
Runtime runtime;
Process process;
String s=null;
String str=null;
try
{
runtime = Runtime.getRuntime();
process =runtime.exec("cmd /c start dir >c:/q1.txt");
str=process.toString();
try
{
process.waitFor();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
s = reader.readLine();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
test obj_Sample= new test();
obj_Sample.BZMCreateZone();
}
}
Thankx