這是一個使用java代碼調用dos命令的實例,在這里我就不多說,直接上代碼,代碼如下:
復制代碼代碼如下:
import java.io.*;
/**
* Java調用windows的DOS命令
* 實現調用Windows的ipconfig命令,然后將輸出的信息通過IO流輸出到控制臺。
*/
public class RunWindowsCommand{
public static void main(String[] args) {
InputStream ins = null;
String[] cmd = new String[] { "cmd.exe", "/C", "ipconfig" }; // 命令
try {
Process process = Runtime.getRuntime().exec(cmd);
ins = process.getInputStream(); // 獲取執行cmd命令后的信息
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line); // 輸出
}
int exitValue = process.waitFor();
System.out.println("返回值:" + exitValue);
process.getOutputStream().close(); // 不要忘記了一定要關
} catch (Exception e) {
e.printStackTrace();
}
}
}