how to print an output stream on console.

Hi,

I am new to JAVA. I am trying to execute a command in runtime and I want to print the output on console and also redirect the output to a file.

Could anyone please help me?

This is what I wrote :

Runtime rt = Runtime.getRuntime();

Process proc = rt.exec("cmd /c ipconfig");

I am getting an output stream by : proc.getOutputStream(). I dont know how to print it on console and also how to redirect it to a file.

Could anyone please help me.

Thanks in advance.

Basav

[556 byte] By [Basav1985a] at [2007-11-27 10:35:35]
# 1

Follow the advice given in

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

sabre150a at 2007-7-28 18:35:52 > top of Java-index,Java Essentials,New To Java...
# 2

You can do it this way :-

public static void main(String args[]) throws Exception {

ProcessBuilder builder = new ProcessBuilder("ipconfig", "/all");

Process process = builder.start();

BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

FileWriter writer = new FileWriter(new File("C:/ipconfig.txt"));

String str;

while ((str = reader.readLine()) != null) {

System.out.println(str);

writer.write(str + System.getProperty("line.separator"));

}

writer.close();

reader.close();

}

Let me know if it works for you.

Brucesworda at 2007-7-28 18:35:52 > top of Java-index,Java Essentials,New To Java...