zoukankan      html  css  js  c++  java
  • Runtime.getRuntime().exec()----记录日志案例

    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找到的button
            MyLog.isDebug = true;//debug模式开启
           conLog.setOnClickListener(this);
        }
    	public void onClick(View v) {
    		//button被点击到了。收集收集日志
    		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("
    ");//每一行一个换行符
    		}
    		//吐司
    		Toast.makeText(this, sb.toString(), 1000).show();
    	}
    }
    对于Logcat的命令能够參考博客:http://www.jb51.net/article/47055.htm

  • 相关阅读:
    linux 防火墙相关
    .net安装windows服务和生产webservice
    linq小实例
    C# DataTable 总结
    document对象
    结对-四则运算-开发过程
    课后作业-阅读任务-阅读笔记-1
    课后作业-阅读任务-阅读提问-2
    课后作业-阅读任务-阅读提问-3
    团对-象棋游戏-开发环境搭建过程
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/6780306.html
Copyright © 2011-2022 走看看