1. 程式人生 > 實用技巧 >Java實現獲取命令列中的指定資料

Java實現獲取命令列中的指定資料

構造一個ping的命令類
這個類中可以設定需要ping的目標域名
類提供方法public void exec();
方法執行完畢後可以讀取ping的次數,ping的成功迴應包個數
ping的丟包個數,ping的丟包率
最大的ping值,最小ping值,平均ping值
ExecPing ep=new ExecPing("www.baidu.com","-n","100");
ep.exec();
ep.getLostPer();
ep.getLostNum();
eo.getMaxPingVal();
eo.getMinPingVal();

主方法中可以輸入:www.baidu.com ,也可以輸入:www.baidu.com","-n","100",所以在功能類中定義了一個一參構造和多參構造,如果是一參則設定預設傳送4次,如果是多參則將引數轉為整形賦給sentNum變數,執行過程主要使用Runtime,將執行結果賦給process,並使用語句new BufferedReader(new InputStreamReader(p.getInputStream()));建立一個物件後使用迴圈依次取出。之後通過字串的indexOf、subString,方法後使用spilt對字串進行分割,之後存入陣列,取陣列第一個元素即可。其餘功能類似。

  1 package day14_1;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.InputStreamReader;
  5 
  6 public class ExecPing {
  7 
  8     private int sentNum;
  9     private int receiveNum;
 10     private int lostPer;
 11     private int lostNum;
 12     private int maxPingVal;
 13     private
int minPingVal; 14 private String cmdStr; 15 16 public ExecPing() { 17 } 18 19 /** 20 * 一參構造 輸入:ping www.baidu.com 21 * 22 * @param cmdStr 23 */ 24 public ExecPing(String cmdStr) { 25 // this.cmdStr = cmdStr; 26 this.cmdStr = "ping " + cmdStr;
27 this.sentNum = 4; 28 } 29 30 /** 31 * 多參構造 輸入:ping -n 10 www.baidu.com 32 * 33 * @param cmdStr 34 * ping www.baidu.com 35 * @param paraOne 36 * -n 37 * @param paraTwo 38 * 10 39 */ 40 // www.baidu.com","-n","100" 41 public ExecPing(String cmdStr, String paraOne, String paraTwo) { 42 this.cmdStr = "ping " + paraOne + " " + paraTwo + " " + cmdStr; 43 this.sentNum = Integer.parseInt(paraTwo); 44 45 } 46 47 /** 48 * 執行方法 49 * 50 * @return 51 */ 52 public void exec() { 53 BufferedReader br = null; 54 StringBuilder sb = new StringBuilder(); 55 try { 56 Process p = Runtime.getRuntime().exec(cmdStr); 57 br = new BufferedReader(new InputStreamReader(p.getInputStream())); 58 String line = null; 59 // StringBuilder sb = new StringBuilder(); 60 while ((line = br.readLine()) != null) { 61 sb.append(line + "\n"); 62 } 63 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } finally { 67 if (br != null) { 68 try { 69 br.close(); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } 73 } 74 } 75 76 String str = sb.toString(); 77 System.out.println(str); 78 79 int indexLostNum = str.indexOf("丟失 = "); 80 String strLostNum = str.substring(indexLostNum + 5); // 找到位置 81 String[] strLostNumArr = strLostNum.split(" ");// 拆分擷取的字串,取第一個內容 82 setLostNum(Integer.parseInt(strLostNumArr[0])); 83 // System.out.println("丟失了:" + Integer.parseInt(strLostNumArr[0]) + 84 // "個"); 85 86 int indexLostPerNum = str.indexOf("丟失 = 0 ("); 87 String strLostPerNum = str.substring(indexLostPerNum + 8); // 找到位置 88 String[] strLostPerNumArr = strLostPerNum.split("%");// 拆分擷取的字串,取第一個內容 89 setLostPer(Integer.parseInt(strLostPerNumArr[0])); 90 91 int indexMaxPingVal = str.indexOf("最長 = "); 92 String strMaxPingVal = str.substring(indexMaxPingVal + 5); // 找到位置 93 String[] strMaxPingValArr = strMaxPingVal.split("m");// 拆分擷取的字串,取第一個內容 94 setMaxPingVal(Integer.parseInt(strMaxPingValArr[0])); 95 96 int indexMinPingVal = str.indexOf("最短 = "); 97 String strMinPingVal = str.substring(indexMinPingVal + 5); // 找到位置 98 String[] strMinPingValArr = strMinPingVal.split("m");// 拆分擷取的字串,取第一個內容 99 setMinPingVal(Integer.parseInt(strMinPingValArr[0])); 100 101 } 102 103 public void getSentNum() { 104 System.out.println("ping了:" + sentNum + "次"); 105 } 106 107 public void getLostPer() { 108 System.out.println("丟失率:" + lostPer + "%"); 109 } 110 111 public void setLostPer(int lostPer) { 112 this.lostPer = lostPer; 113 } 114 115 public void getLostNum() { 116 System.out.println("丟失了:" + lostNum + "個"); 117 } 118 119 public void setLostNum(int lostNum) { 120 this.lostNum = lostNum; 121 } 122 123 public void getMaxPingVal() { 124 System.out.println("最長:" + maxPingVal + "毫秒"); 125 } 126 127 public void setMaxPingVal(int maxPingVal) { 128 this.maxPingVal = maxPingVal; 129 } 130 131 public void getMinPingVal() { 132 System.out.println("最短:" + minPingVal + "毫秒"); 133 } 134 135 public void setMinPingVal(int minPingVal) { 136 this.minPingVal = minPingVal; 137 } 138 139 }
 1 package day14_1;
 2 
 3 public class TestExecPing {
 4     public static void main(String[] args) {
 5          ExecPing ep = new ExecPing("www.baidu.com","-n","2");
 6             ep.exec();
 7             System.out.println("======================================");
 8             ep.getLostNum();
 9             ep.getLostPer();
10             ep.getMaxPingVal();
11             ep.getMinPingVal();
12             ep.getSentNum();
13     }
14 
15 }

執行截圖如下: