黑马程序员_利用Java调用可执行命令

----------- android培训java培训、java学习型技术博客、期待与您交流! ------------
  1. ProcessBuilder类执行外部的程序,相对于 Runtime.exec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址  
  2.     
  3.   package com.kuaff.jdk5package;  
  4.     
  5.   import java.io.IOException;  
  6.   import java.io.InputStream;  
  7.   import java.util.ArrayList;  
  8.   import java.util.List;  
  9.     
  10.   public class ProcessBuilderShow  
  11.   {  
  12.   public static List getPhysicalAddress()  
  13.   {  
  14.   Process p = null;  
  15.   //物理网卡列表  
  16.   List address = new ArrayList();  
  17.     
  18.   try  
  19.   {  
  20.   //执行ipconfig /all命令  
  21.   p = new ProcessBuilder("ipconfig""/all").start();  
  22.   }  
  23.   catch (IOException e)  
  24.   {  
  25.   return address;  
  26.   }  
  27.   byte[] b = new byte[1024];  
  28.   StringBuffer sb = new StringBuffer();  
  29.   //读取进程输出值  
  30.   InputStream in = p.getInputStream();  
  31.   try  
  32.   {  
  33.   while (in.read(b)>0)  
  34.   {  
  35.   sb.append(new String(b));  
  36.   }  
  37.   }  
  38.   catch (IOException e1)  
  39.   {  
  40.   }  
  41.   finally  
  42.   {  
  43.   try  
  44.   {  
  45.   in.close();  
  46.   }  
  47.   catch (IOException e2)  
  48.   {  
  49.   }  
  50.   }  
  51.   //以下分析输出值,得到物理网卡  
  52.   String rtValue = sb.substring(0);  
  53.   int i = rtValue.indexOf("Physical Address. . . . . . . . . :");  
  54.   while(i>0)  
  55.   {  
  56.   rtValue = rtValue.substring(i + "Physical Address. . . . . . . . . :".length());  
  57.   address.add(rtValue.substring(0,18));  
  58.   i = rtValue.indexOf("Physical Address. . . . . . . . . :");  
  59.   }  
  60.   return address;  
  61.   }  
  62.   public static void main(String[] args)  
  63.   {  
  64.   List address = ProcessBuilderShow.getPhysicalAddress();  
  65.   for(String add:address)  
  66.   {  
  67.   System.out.printf("物理网卡地址:%s%n", add);  
  68.   }  
  69.   }  
  70.   } </p>  
Java代码   收藏代码
  1. <pre name="code" class="java">/** 
  2.  * Add one sentence class summary here. 
  3.  * Add class description here. 
  4.  * 
  5.  * @author lxx 
  6.  * @version 1.0, 2004-11-16 
  7.  */  
  8. public class TestCmd {  
  9.     public TestCmd(){}  
  10.    /* public  void main(String args[]){ 
  11.         try { 
  12.           Process process = Runtime.getRuntime().exec("cmd.exe  /c  start  http://www.csdn.net");  //登录网站 
  13.           Process process = Runtime.getRuntime().exec("cmd.exe  /c  start  ping 10.144.98.100");  //调用Ping命令 
  14.         }catch (Exception  e) 
  15.         { 
  16.             e.printStackTrace(); 
  17.             }   
  18.          
  19.       } 
  20.     }  */  
  21.   
  22. //在项目下建立一个名为hello的文件夹  
  23.     public static void main(String[] args) {  
  24.         System.out.println(System.getProperty("user.dir"));  
  25.         createFolder("hello");  
  26.       }  
  27.   
  28.      private static void createFolder(String folderName) {  
  29.        String temp = System.getProperty("user.dir") + java.io.File.separator+ folderName;  
  30.        java.io.File f = new java.io.File(temp);  
  31.        f.mkdirs();  
  32.      }  
  33.   
  34.  }  
  35.   
  36.   
  37.   
  38. 在Java程序中获取当前运行程序的路径  
  39.   
  40. import java.io.*;  
  41.   
  42. public class Test {  
  43.   
  44.        public static void main(String[] args) {  
  45.   
  46.               File directory  = new File(".");  
  47.   
  48.               try {  
  49.   
  50.                      File newPath = new File(directory.getCanonicalPath()+"NewFolder");  
  51.   
  52.                      newPath.mkdir();  
  53.   
  54.               }catch(Exception exp)  
  55.   
  56.               {  
  57.   
  58.                      exp.printStackTrace();  
  59.   
  60.               }  
  61.   
  62.        }  
  63.   
  64. }  
  65.   
  66. //File directory       = new File(".");  
  67.   
  68. //directory.getCanonicalPath();取得当前路径  
  69.   
  70.   
  71. 在Jsp页面中调用Ping命令---PingIP.jsp  
  72.   
  73. <%@ page language="java" contentType="text/html; charset=gb2312"  import="java.io.*" %>  
  74. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  75. <html>  
  76. <head>  
  77. <title>Ping IP测试页面</title>  
  78. </head>  
  79.   
  80. <body>  
  81. <div align="center">  
  82.   <h2>Ping IP测试页面</h2>  
  83. </div>   
  84.   
  85. <%    
  86.    Runtime  runtime  =  Runtime.getRuntime();    
  87.    Process  process  =null;    
  88.    String  line=null;    
  89.    InputStream  is  =null;    
  90.    InputStreamReader  isr=null;    
  91.    BufferedReader  br  =null;    
  92.    String  ip="www.sina.com.cn";  //待Ping的地址  
  93.     try    
  94.    {    
  95.        process  =runtime.exec("ping  "+ip);    
  96.        is  =  process.getInputStream();    
  97.        isr=new  InputStreamReader(is);    
  98.        br  =new  BufferedReader(isr);    
  99.        out.println("<pre>");    
  100.        while(  (line  =  br.readLine())  !=  null  )    
  101.        {    
  102.            out.println(line);    
  103.            out.flush();    
  104.        }    
  105.        out.println("</pre>");    
  106.        is.close();    
  107.        isr.close();    
  108.        br.close();    
  109.    }    
  110.    catch(IOException  e  )    
  111.    {    
  112.        out.println(e);    
  113.        runtime.exit(1);    
  114.    }    
  115. %>    
  116.   
  117. </body>  
  118. </html>  
  119. </pre>  
  120. <p> </p>  
  121.    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值