標籤:stream final flush comm puts des shell .sh plain
在java程式中運行shell命令,改變檔案的許可權。能夠在命令列中運行
chmod 777 <span style="font-family: Arial, Helvetica, sans-serif;">/data/misc/123.sh"</span>
來改變許可權,可是在java代碼中運行這個命令時使用
Runtime.getRuntime().exec("chmod 777 /data/misc/123.sh");
無效,使用
String[] command = new String[] {"/system/bin/sh","-c","chmod 777 /data/misc/123.sh"};Runtime.getRuntime().exec(command);
相同無效
最後通過執行個體化一個dataoutputstream對象,在這個對象的寫位元組方法裡寫命令才實現,代碼例如以下:
String[] commands = new String[] { "/system/bin/sh", "-c","chmod -R 777 /data/misc/123.sh" };Process process = null;DataOutputStream dataOutputStream = null;try {process = Runtime.getRuntime().exec("su");dataOutputStream = new DataOutputStream(process.getOutputStream());int length = commands.length;for (int i = 0; i < length; i++) {dataOutputStream.writeBytes(commands[i] + "\n");}dataOutputStream.writeBytes("exit\n");dataOutputStream.flush();process.waitFor();} catch (Exception e) {} finally {try {if (dataOutputStream != null) {dataOutputStream.close();}process.destroy();} catch (Exception e) {}}
java運行shell命令,chmod 777 xxx,改變許可權無效的解決的方法。