Java调用外部C语言程序,进行标准输入输出操作

import java.lang.*;
class test3{
       public static void main(String []args) throws IOException{
                long a = System.currentTimeMillis();
                Process process = Runtime.getRuntime().exec("/home/xxx");//这是外部程序所在目录,切记返回类型为process
                PrintStream outputWriter = new PrintStream(new BufferedOutputStream(process.getOutputStream()));
                outputWriter.println("XXXX");
                outputWriter.flush();//这里一定要刷新缓冲区,不然参数传不过去
                outputWriter.println("XXX");
                outputWriter.flush();
                outputWriter.println("XXX");
                outputWriter.flush();
                outputWriter.println("XXX");
                outputWriter.flush();
                outputWriter.println("XXX");
                outputWriter.flush();
                BufferedReader addResult = new BufferedReader(
                                                              new InputStreamReader(process.getInputStream())
                                                              );
                String line;
                while((line=addResult.readLine())!=null)
                {
                     System.out.println(line);
                 }
                long b = System.currentTimeMillis();
                System.out.println(b-a);
}
}



你可能感兴趣的:(Java调用外部C语言程序,进行标准输入输出操作)