Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Redirecting command line output to a text file using java program

807589Jan 2 2009 — edited Jan 2 2009
Hi 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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 30 2009
Added on Jan 2 2009
4 comments
1,185 views