一个执行CMD命令的工具类

  1. package com.one.two;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6.   
  7. public class Runtimetest {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub  
  14.         Runtimetest rr=new Runtimetest();  
  15.         String res=rr.exeRuntimeCmd("ipconfig");//输入命令  
  16.         System.out.println(res);  
  17.   
  18.     }  
  19.   
  20.     /** 
  21.      * 执行CMD命令 
  22.      *  
  23.      */  
  24.     public String exeRuntimeCmd(String cmd) {  
  25.         BufferedReader bufferedReader = null;  
  26.         Process process = null;  
  27.         StringBuffer sb = new StringBuffer();  
  28.         try {  
  29.             String buffer;  
  30.             process = Runtime.getRuntime().exec(cmd);  
  31.             // 得到返回结果  
  32.             bufferedReader = new BufferedReader(new InputStreamReader(  
  33.                     process.getInputStream(),"gbk" ));  
  34.             while ((buffer = bufferedReader.readLine()) != null) {  
  35.                 sb.append(buffer + "\n");  
  36.             }  
  37.             return sb.toString();  
  38.         } catch (IOException e) {  
  39.             System.out.println("IOException IO异常");  
  40.             return null;  
  41.         } finally {  
  42.             try {  
  43.                 if (bufferedReader != null) {  
  44.                     bufferedReader.close();  
  45.                     bufferedReader = null;  
  46.                 }  
  47.   
  48.                 if (process != null) {  
  49.                     process.destroy();  
  50.                     process = null;  
  51.                 }  
  52.             } catch (Exception e) {  
  53.                 e.printStackTrace();  
  54.             }  
  55.         }  
  56.     }  
  57.   
  58. }  


控制台打印:



Windows IP Configuration





Ethernet adapter 本地连接 2:



        Connection-specific DNS Suffix  . : 

        IP Address. . . . . . . . . . . . : 192.168.16.24

        Subnet Mask . . . . . . . . . . . : 255.255.255.0

        IP Address. . . . . . . . . . . . : fe80::ea9a:8fff:fe5f:e4de%4

        Default Gateway . . . . . . . . . : 192.168.16.1



Tunnel adapter Teredo Tunneling Pseudo-Interface:



        Connection-specific DNS Suffix  . : 

        IP Address. . . . . . . . . . . . : fe80::ffff:ffff:fffd%5

        Default Gateway . . . . . . . . . : 



Tunnel adapter Automatic Tunneling Pseudo-Interface:



        Connection-specific DNS Suffix  . : 

        IP Address. . . . . . . . . . . . : fe80::5efe:192.168.16.24%2

        Default Gateway . . . . . . . . . : 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值