Tuesday, February 23, 2010

Reading console output in Java

Have you ever needed to read console output from a Java program??
here is a simple code which shows how to read console output -


import java.io.*;
class ConsoleReader
{
 public static void main(String args[]) throws Exception
 {
  Process p=Runtime.getRuntime().exec("ping www.google.com");
  BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
  String str="";
  while(str!=null)
  {
   System.out.println(str);
   str=br.readLine();
  }
  br.close();
 }
}

No comments:

Post a Comment

Ads Inside Post