Runtime.getRuntime().exec()----記錄日誌案例,runtime.getruntime

來源:互聯網
上載者:User

Runtime.getRuntime().exec()----記錄日誌案例,runtime.getruntime
Runtime.getRuntime().exec()方法主要用於執行外部的程式或命令。
Runtime.getRuntime().exec共有六個重載方法:
1.public Process exec(String command)
在單獨的進程中執行指定的字串命令。
2.public Process exec(String [] cmdArray)
在單獨的進程中執行指定命令和變數
3.public Process exec(String command, String [] envp)
在指定環境的獨立進程中執行指定命令和變數
4.public Process exec(String [] cmdArray, String [] envp)
在指定環境的獨立進程中執行指定的命令和變數
5.public Process exec(String command,String[] envp,File dir)
在有指定環境和工作目錄的獨立進程中執行指定的字串命令
6.public Process exec(String[] cmdarray,String[] envp,File dir)
在指定環境和工作目錄的獨立進程中執行指定的命令和變數

我們先來比較exec(String command)與exec(String[] cmdArray)的區別,其實他們是等價的,最終都會調用:
exec(String[] cmdarray,String[] envp,File dir),我們看看方法exec(String cmdarray,String[] envp,File dir) throws IOException的實現代碼:

public Process exec(String command, String[] envp, File dir) throws IOException {    if (command.length() == 0) throw new IllegalArgumentException("Empty command");    StringTokenizer st = new StringTokenizer(command);    String[] cmdarray = new String[st.countTokens()];    for (int i = 0; st.hasMoreTokens(); i++)        cmdarray[i] = st.nextToken();    return exec(cmdarray, envp, dir);}

從上面的代碼,我們可以看出最終調用的代碼都是:exec(String[] cmdArray,String envp,File  dir)。exec(String command)相當於exec(command,null,null),exec(String[] cmdArray)相當於exec(cmdArray,null,null)。


參數說明:
cmdarray - 包含所調用命令及其參數的數組。
envp - 字串數組,其中每個元素的環境變數的設定格式為 name=value,如果子進程應該繼承當前進程的環境,或該參數為 null。
dir - 子進程的工作目錄;如果子進程應該繼承當前進程的工作目錄,則該參數為 null。

另外,執行exec(String command)不等同於直接執行command line命令,比如命令:
javap -l xxx > output.txt
這時要用exec(String[] cmdArray)。如例:
Process p = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",

    "javap -l xxx > output.txt"});


關於返回結果類型:Process,它有幾個方法:
1.destroy():殺掉子進程
2.exitValue():返回子進程的出口值,值 0 表示正常終止
3.getErrorStream():擷取子進程的錯誤流
4.getInputStream():擷取子進程的輸入資料流

5.getOutputStream():擷取子進程的輸出資料流

6.waitFor():導致當前線程等待,如有必要,一直要等到由該 Process 對象表示的進程已經終止。如果已終止該子進程,此方法立即返回。如果沒有終止該子進程,調用的線程將被阻塞,直到退出子進程,根據慣例,0 表示正常終止


對Runtime.getRuntime.exec()的瞭解可以參考部落格:http://www.cnblogs.com/mingforyou/p/3551199.html

案列:實現Log中的日誌顯示(以對話方塊的形式示範,可以將輸出日誌儲存到具體的檔案中)

public class MainActivity extends Activity implements OnClickListener{    Button conLog;/** Called when the activity is first created. */    @Override     public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        conLog = (Button) findViewById(R.id.conLog);//通過id找到的按鈕        MyLog.isDebug = true;//debug模式開啟       conLog.setOnClickListener(this);    }public void onClick(View v) {//按鈕被點擊到了,收集收集日誌try {readLog();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * @author Danny QQ 858030348   * @throws IOException  *  */private void readLog() throws IOException {MyLog.i("INFO", "start connectLog");StringBuffer sb = new StringBuffer();ArrayList<String> cmdLine = new ArrayList<String>();cmdLine.add("logcat");cmdLine.add("-d");//收集一次日誌停止cmdLine.add("-s");//過濾cmdLine.add("INFO");System.out.println(cmdLine.toArray(new String[cmdLine.size()]));Process exec = Runtime.getRuntime().exec(cmdLine.toArray(new String[cmdLine.size()]));//擷取執行命令後的輸入資料流InputStream inputStream = exec.getInputStream();InputStreamReader buInputStreamReader = new InputStreamReader(inputStream);//裝飾器模式BufferedReader bufferedReader = new BufferedReader(buInputStreamReader);//直接讀字串String str = null;while((str = bufferedReader.readLine())!=null){sb.append(str);//每讀一行拼接到sb裡面去sb.append("\n");//每一行一個分行符號}//多士Toast.makeText(this, sb.toString(), 1000).show();}}
對於Logcat的命令可以參考部落格:http://www.jb51.net/article/47055.htm

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.