java調用控制台命令


String shell = "ping www.baidu.com";//需要執行的命令

BufferedReader br = null;
try {
Process p = Runtime.getRuntime().exec(shell);//調用控制台執行shell
br = new BufferedReader(new InputStreamReader(p.getErrorStream()));//獲取執行后出現的錯誤;getInputStream是獲取執行后的結果

String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
System.out.println(sb);
}
System.out.println(sb);//打印執行后的結果
} catch (Exception e) {
e.printStackTrace();
}
finally
{
if (br != null)
{
try {
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM