ITPub博客

首页 > 应用开发 > Java > 使用Java调用Windows的DOS命令

使用Java调用Windows的DOS命令

Java 作者:ubuntulinuxjava 时间:2015-02-12 14:01:18 0 删除 编辑
来源:http://www.bug315.com/article/109.htm

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();
       }
       
   }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26875129/viewspace-1434264/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: MySQL range分区
请登录后发表评论 登录
全部评论

注册时间:2015-01-20

  • 博文量
    12
  • 访问量
    19439