Java核心代码例程之:ProcessDemo.java

发表于:2007-06-22来源:作者:点击数: 标签:
import java .io.*; /** *Demonstrateshowtorunaprogramandreaditsoutput ***/ publicclassProcessDemo { publicstaticvoidmain(Stringargs[])throwsException { Processprocess=Runtime.getRuntime().exec(ls); InputStreamstandardOutput=process.getInput

   
import java.io.*;


/**
 * Demonstrates how to run a program and read it"s output
 ***/
public class ProcessDemo
{

    public static void main(String args[]) throws Exception
    {
        Process process = Runtime.getRuntime().exec("ls");
        InputStream standardOutput = process.getInputStream(),
                    standardError  = process.getErrorStream();
                   
        int c;
        System.out.println("
[ Standard Output ]");
        while ((c = standardOutput.read()) != -1)
           System.out.print((char)c);
           
        System.out.println("
[ Standard Error ]");
        while ((c = standardError.read()) != -1)
           System.out.print((char)c);
           
        standardOutput.close();
        standardError.close();
    }
}

原文转自:http://www.ltesting.net