用Java读取Windows的Command指令

java 代码
  1. /***********************************************************************  
  2. * Module:  MACAddress.java  
  3. * Author:  danlley  
  4. * Modified: 2006年6月05日 12:45:21  
  5. * Purpose: Defines the Interface ManageCA  
  6. ***********************************************************************/  
  7.   
  8. import java.io.BufferedReader;   
  9. import java.io.InputStreamReader;   
  10. import java.io.IOException;   
  11. import java.util.ArrayList;   
  12.   
  13. public class MACAddress {   
  14.     public MACAddress() {   
  15.     }   
  16.   
  17.     public static ArrayList getMACAddress() {   
  18.         ArrayList arrayList=new ArrayList();   
  19.         String address = "";   
  20.         String os = System.getProperty("os.name");   
  21.         if (os != null && os.startsWith("Windows")) {   
  22.             try {   
  23.                 String command = "cmd.exe /c ipconfig /all";   
  24.                 Process p = Runtime.getRuntime().exec(command);   
  25.                 BufferedReader br =   
  26.                         new BufferedReader(   
  27.                                 new InputStreamReader(p.getInputStream()));   
  28.                 String line;   
  29.                 while ((line = br.readLine()) != null) {   
  30.                     if(line.trim().length()>0){   
  31.                         arrayList.add(line);   
  32.                     }   
  33.                 }   
  34.                 int size=arrayList.size();   
  35.                 System.out.println("Read from InputStream the List size is : "+size);   
  36.                 br.close();   
  37.                 return arrayList;   
  38.             } catch (IOException e) {}   
  39.         }   
  40.         return arrayList;   
  41.     }   
  42.   
  43.     public static void main(String[] args) {   
  44.         ArrayList arrayList=MACAddress.getMACAddress();   
  45.         int size=arrayList.size();   
  46.         System.out.println("Get the data from the list the size is : "+size);   
  47.         for(int i=0;i0){   
  48.                 String str=arrayList.get(i).toString().trim();   
  49.                 int len=str.length();   
  50.                 int pointIndex=str.indexOf(":");   
  51.                 //System.out.println("Line "+i+" Len="+len+" pointIndex="+pointIndex+" "+str);   
  52.                 if(pointIndex>0){   
  53.                     String subStr=str.substring(pointIndex+1,len).trim();   
  54.                     int lenSubStr=subStr.length();   
  55.                     if(lenSubStr>0){   
  56.                         System.out.println(subStr);   
  57.                     }   
  58.                 }   
  59.             }   
  60.         }   
  61.     }   
  62. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值