without permission, no reprint!!!
Packagepractice;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader; Public classcmdadb { Public voidexecutecmdconsole (String cmd) {//This method prints the log to the console!!!!!!!!!!!! //This method runs successfully!!! System.out.println ("Enter in cmd" +cmd); Process p; Try{p=runtime.getruntime (). exec (CMD); System.out.println ("::::::::::::::::::"::::::::::::::::::::::>>>>>> "start in the console print log"); //p.waitfor ();BufferedReader breader=NewBufferedReader (NewInputStreamReader (P.getinputstream (), "GBK")); String Line=NULL; while((Line=breader.readline ())! =NULL) System.out.println (line); } Catch(IOException e) {e.printstacktrace (); } } PublicString Executecmdfile (string[] cmmands, String logtofile, String dirtodocmd)throwsIOException {//This method outputs the log to the specified folder!!!!!!!!!!!! //This method runs successfully!!! //if string Cmmand string cmmand = "adb logcat-v time > D:/adb.log"; //string[] cmmands so String commands[] = {"adb", "Logcat", "-V", "Time"}; //String Logtofile Save the journal to Logtofile//String dirtodocmd execute cmd command in Dirtodocmd//because the log is output to the file, you can no longer send the journal to the console Try{Processbuilder Builder=NewProcessbuilder (cmmands); if(Dirtodocmd! =NULL) Builder.directory (NewFile (dirtodocmd)); Builder.redirecterrorstream (true); Builder.redirectoutput (NewFile (logtofile)); Process Process=Builder.start (); Process.waitfor (); //get the result after the command executesInputStream is =Process.getinputstream (); BufferedReader Buffer=NewBufferedReader (NewInputStreamReader (IS, "GBK")); String Line=NULL; StringBuffer Sbbuffer=NewStringBuffer (); while(line = Buffer.readline ())! =NULL) {sbbuffer.append (line); } is.close (); returnsbbuffer.tostring (); } Catch(Exception e) {e.printstacktrace (); } return NULL; }
Public Static voidMain (string[] args)throwsIOException {//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 =Newcmdadb (); 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), because the log is output to the file, you can no longer log out to the console }}
Java calls execute cmd command