在Java中丢失来自bash脚本的数据

时间:2010-07-12 00:52:05

标签: java bash

我试图将bash数据转换为变量。问题是,它是如此随意。该命令每次执行,我可以通过启动X应用程序来看到。但是,我的处理器可能太快或太慢而无法发出echo命令并启动对输入流的缓冲读取。

我怎样才能让它发挥作用?我需要以某种方式在缓冲读卡器内发出命令。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class test1 {

 public static void main() {

  try { 
   Process proc = Runtime.getRuntime().exec("echo Gosh, I sure hope this comes back to java");
   BufferedReader read = new BufferedReader(new InputStreamReader(proc
     .getInputStream()));

   while (read.ready()) {
    System.out.println(read.readLine());
   }
  } catch (IOException e) {
   System.out.println(e.getMessage());
  }
 }

2 个答案:

答案 0 :(得分:1)

阅读this或尝试java.lang.Process

答案 1 :(得分:0)

用这个替换你的while循环:

String line;
while ((line = read.readLine())!=null) {
    System.out.println(line);
}