码迷,mamicode.com
首页 > 编程语言 > 详细

Java调用Linux命令

时间:2016-12-02 22:21:56      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:input   exit   while   命令   nbsp   输出   buffer   runtime   run   

 

// int tp = 1 返回执行结果  非1 返回命令执行后的输出
public static String runCommand(String cmd, int tp) {
StringBuffer buf = new StringBuffer(1000);
String rt = "-1";
try {
Process pos = Runtime.getRuntime().exec(cmd);
pos.waitFor();
if (tp == 1) {
if (pos.exitValue() == 0) {
rt = "1";
}
} else {
InputStreamReader ir = new InputStreamReader(pos.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String ln = "";
while ((ln = input.readLine()) != null) {
buf.append(ln + "<br>");
}
rt = buf.toString();
input.close();
ir.close();
}
} catch (java.io.IOException e) {
rt = e.toString();
} catch (Exception e) {
rt = e.toString();
}
return rt;
}

Java调用Linux命令

标签:input   exit   while   命令   nbsp   输出   buffer   runtime   run   

原文地址:http://www.cnblogs.com/haorun/p/6127016.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!