廢話不多說了,上代碼:
package com.one.two;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class Runtimetest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubRuntimetest rr=new Runtimetest();String res=rr.exeRuntimeCmd("ipconfig");//輸入命令System.out.println(res);}/** * 執行CMD命令 * */public String exeRuntimeCmd(String cmd) {BufferedReader bufferedReader = null;Process process = null;StringBuffer sb = new StringBuffer();try {String buffer;process = Runtime.getRuntime().exec(cmd);// 得到返回結果bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(),"gbk" ));while ((buffer = bufferedReader.readLine()) != null) {sb.append(buffer + "\n");}return sb.toString();} catch (IOException e) {System.out.println("IOException IO異常");return null;} finally {try {if (bufferedReader != null) {bufferedReader.close();bufferedReader = null;}if (process != null) {process.destroy();process = null;}} catch (Exception e) {e.printStackTrace();}}}}
控制台列印:
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 . . . . . . . . . :