标签:runtime.getruntime.e exec 记录日志
Runtime.getRuntime().exec()方法主要用于执行外部的程序或命令。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)。
"javap -l xxx > output.txt"});
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 block e.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
Runtime.getRuntime().exec()----记录日志案例
标签:runtime.getruntime.e exec 记录日志
原文地址:http://blog.csdn.net/itjavawfc/article/details/46667213