一個執行CMD命令的工具類

來源:互聯網
上載者:User

廢話不多說了,上代碼:

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 . . . . . . . . . :

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.