首页 > 代码库 > [Java] Runtime Exec

[Java] Runtime Exec

经常用到Runtime的exec方法,记录一下标准用法,便于以后复制代码。

        Process process;        String cmd = "cat /sys/devices/virtual/mxc_edid_class/mxc_edid_dev/timing";        StringBuffer sb = new StringBuffer("");        try {            process = Runtime.getRuntime().exec("su");            String line = null;            DataOutputStream dos = new DataOutputStream(                    process.getOutputStream());            dos.writeBytes(cmd + "\n");            dos.flush();            dos.writeBytes("exit\n");            dos.flush();            BufferedReader bf = new BufferedReader(new InputStreamReader(                    process.getErrorStream()));            while ((line = bf.readLine()) != null) {            }            BufferedReader bf2 = new BufferedReader(new InputStreamReader(                    process.getInputStream()));            while ((line = bf2.readLine()) != null) {                sb.append(line);            }            process.waitFor();        } catch (Exception e) {            e.printStackTrace();            return;        }

 

[Java] Runtime Exec