zoukankan      html  css  js  c++  java
  • java执行cmd命令并获取输出结果

    1.java执行cmd命令并获取输出结果

     1 import java.io.BufferedReader;
     2 import java.io.InputStreamReader;
     3 
     4 import org.apache.commons.lang3.text.StrBuilder;
     5 
     6 /**
     7  *
     8  * @author user1
     9  */
    10 public class DeleteProgram {
    11     public static void run() {
    12         Runtime runtime = Runtime.getRuntime();
    13         try {
    14             BufferedReader br = new BufferedReader(new InputStreamReader(runtime.exec("ipconfig").getInputStream()));
    15             //StringBuffer b = new StringBuffer();
    16             String line=null;
    17             StringBuffer b=new StringBuffer();
    18             while ((line=br.readLine())!=null) {
    19                 b.append(line+"
    ");
    20             }
    21             System.out.println(b.toString());
    22         } catch (Exception e) {
    23             e.printStackTrace();
    24         }
    25 
    26     }
    27 
    28     public static void main(String[] args) {
    29         DeleteProgram delp = new DeleteProgram();
    30         delp.run();
    31     }
    32 
    33 }

    2.获取您需要的字段

    public static String getbackage(String path){
         Process p;  
            //test.bat中的命令是ipconfig/all 
            String cmd = "aapt dump badging " + path;
    //        String cmd="jarsigner -verify -verbose -certs C:\Users\Administrator\Desktop\PandaClient.apk";  
            String resultstr = null;
            try  
            {  
                //执行命令  
                p = Runtime.getRuntime().exec(cmd);  
                //取得命令结果的输出流  
                InputStream fis=p.getInputStream();  
                //用一个读输出流类去读  
                //用缓冲器读行  
                BufferedReader br=new BufferedReader( new InputStreamReader(fis,"GB2312"));  
                String line=null;  
                //直到读完为止  
                int i = 0;
                while((line=br.readLine())!=null)  
                {  
                 if(line.contains("package:")){  //解析符合自己需要的內容,获取之后,直接返回。
                    
                  resultstr = line;
                      break;
                 }
    //                resultstr=line;
                    i++;
                }  
            }  
            catch (IOException e)  
            {  
                e.printStackTrace();  
            }  
            return resultstr;
    }
    1     public static void main(String[] args) {
    2         String url="www.taobao.com";
    3         String response=null;
    4         response=getSingle(url);
    5         
    6         System.out.println(response);
    7     }
  • 相关阅读:
    Valid Anagram
    Spiral Matrix II
    Spiral Matrix
    Kth Smallest Element in a BST
    Count Primes
    javascript 判断浏览器
    javascript 数值交换技巧
    EntityFramework 6 分页模式
    JSON.parse 和 JSON.stringify
    CSS z-index
  • 原文地址:https://www.cnblogs.com/sunny-sl/p/7527364.html
Copyright © 2011-2022 走看看