码迷,mamicode.com
首页 > 其他好文 > 详细

实现获取命令行的返回结果

时间:2020-07-27 23:57:30      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:out   code   red   inpu   finally   end   nbsp   new   proc   

 1 /**
 2  * @author liuwenlong
 3  * @create 2020-07-24 15:00:39
 4  */
 5 @SuppressWarnings("all")
 6 public class TestRunTime {
 7     public static String exeCmd(String commandStr) {
 8         BufferedReader br = null;
 9         try {
10             Process p = Runtime.getRuntime().exec(commandStr);
11             br = new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));
12             String line = null;
13             StringBuilder sb = new StringBuilder();
14             while ((line = br.readLine()) != null) {
15                 sb.append(line + "\n");
16             }
17             return sb.toString();
18         } catch (Exception e) {
19             e.printStackTrace();
20         } finally {
21             if (br != null) {
22                 try {
23                     br.close();
24                 } catch (Exception e) {
25                     e.printStackTrace();
26                 }
27             }
28         }
29         return commandStr;
30     }
31 
32     public static void main(String[] args) {
33         String commandStr = "ping 127.0.0.1";
34         System.out.println(TestRunTime.exeCmd(commandStr));
35     }
36 }

技术图片

 

实现获取命令行的返回结果

标签:out   code   red   inpu   finally   end   nbsp   new   proc   

原文地址:https://www.cnblogs.com/lwl80/p/13386973.html

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