BufferedReader无法按预期工作

时间:2012-11-28 12:08:00

标签: java bufferedreader

以下代码在此行“bufferedReader.readLine();”之后未完全执行。当我执行系统命令时,程序工作正常 提到远程PC的IPAddress。

class Test    
{       
public static void main(String arg[])    
{    
     Process p;    
     Runtime runTime;    
     String process = null;    
     try {    
        runTime = Runtime.getRuntime();    
        p = runTime.exec("sc \\xx.xx.xx.xx query gpsvc");      // For Windows    
        InputStream inputStream = p.getInputStream();    
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);    
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);    
        String line = bufferedReader.readLine();    
        process = "&";    
        while (line != null) {    
            line = bufferedReader.readLine();    
            process += line + "&";    
        }    
        StringTokenizer st = new StringTokenizer(proc, "&");    
        System.out.println("token size "+st.countTokens());    
        while (st.hasMoreTokens()) {        
            String testData = st.nextToken();    
        bufferedReader.close();    
        inputStreamReader.close();    
        inputStream.close();    
        }    

     } catch (IOException e) {    
       System.out.println("Exception arise during the read Processes");    
       e.printStackTrace();    
     }    

}    
}        

1 个答案:

答案 0 :(得分:1)

检查exec方法

中的命令

p = runTime.exec("sc \\xx.xx.xx.xx query gpsvc");

这里的语法错误,如果从命令提示符执行此操作,系统将提示您输入以下问题。

Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]:

在你输入y或n之前,程序不会返回。由于该程序没有终止,您将无法读取控制台输出,这就是您的程序卡在String line = bufferedReader.readLine();上的原因