java調用執行cmd命令

來源:互聯網
上載者:User

標籤:log   cmd命令   ocm   儲存   runtime   sys   mdf   指定   結果   

未經允許,禁止轉載!!!

package practice;import java.io.BufferedReader;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class cmdadb {        public void executeCMDconsole(String cmd) {        //此方法為列印日誌到控制台!!!!!!!!!!!!        //此方法跑成功!!!                System.out.println("在cmd裡面輸入"+cmd);        Process p;        try {            p = Runtime.getRuntime().exec(cmd);                        System.out.println(":::::::::::::::::::開始在控制台列印日誌::::::::::::::::::::::>>>>>>");            //p.waitFor();            BufferedReader bReader=new BufferedReader(new InputStreamReader(p.getInputStream(),"gbk"));            String line=null;            while((line=bReader.readLine())!=null)                System.out.println(line);        } catch (IOException e) {            e.printStackTrace();        }     }            public String executeCMDfile(String[] cmmands, String logToFile, String dirTodoCMD ) throws IOException {        //此方法為輸出日誌到指定檔案夾!!!!!!!!!!!!        //此方法跑成功!!!        //如果 String cmmand 那麼  String cmmand = "adb logcat -v time > d:/adb.log";        //String[] cmmands 所以   String commands[] = { "adb", "logcat","-v","time"};        //String logToFile  將日誌儲存到logToFile        //String dirTodoCMD 在dirTodoCMD執行cmd命令        //由於將日誌輸出到檔案裡面了,就不能再將日誌輸出到console了                try {            ProcessBuilder builder = new ProcessBuilder(cmmands);            if (dirTodoCMD != null)                builder.directory(new File(dirTodoCMD));            builder.redirectErrorStream(true);            builder.redirectOutput(new File(logToFile));            Process process = builder.start();            process.waitFor();            // 得到命令執行後的結果            InputStream is = process.getInputStream();            BufferedReader buffer = new BufferedReader(new InputStreamReader(is, "gbk"));            String line = null;            StringBuffer sbBuffer = new StringBuffer();            while ((line = buffer.readLine()) != null) {                sbBuffer.append(line);            }                        is.close();            return sbBuffer.toString();        } catch (Exception e) {            e.printStackTrace();        }        return null;    }

public static void main(String[] args) throws IOException { //String cmd="D:/Android/android-sdk-windows/platform-tools/adb logcat -v time"; //String cmd2="adb devices"; //String cmd3="adb logcat -v time"; //String cmd4="adb logcat -v time > d:/adb.log"; cmdadb adbc = new cmdadb(); adbc.executeCMDconsole("adb logcat -v time"); String commands[] = { "adb", "logcat","-v","time"}; adbc.executeCMDfile(commands, "D:/adb.logs", "C:/Users/wb-cjz286752"); //System.out.println(result);由於將日誌輸出到檔案裡面了,就不能再將日誌輸出到console了 }}

 

java調用執行cmd命令

聯繫我們

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