针对linux系统
- 直接使用“su“命令,提高所操作文件权限即可
针对于anroid/java代码中需要改变某些文件的权限,例如读写权限,操作权限等的使用方式(相当如如何java调用linux命令)
public static String exec(String cmd) {
try {
if (cmd != null) {
Runtime rt = Runtime.getRuntime();
Process process = rt.exec("su");//Root //Process process = rt.exec("sh");//
DataOutputStream dos = new DataOutputStream(process.getOutputStream());
dos.writeBytes(cmd + "\n");
dos.flush();
dos.writeBytes("exit\n");
dos.flush();
InputStream myin = process.getInputStream();
InputStreamReader is = new InputStreamReader(myin);
char[] buffer = new char[1024];
int bytes_read = is.read(buffer);
StringBuffer aOutputBuffer = new StringBuffer();
while (bytes_read > 0) {
aOutputBuffer.append(buffer, 0, bytes_read);
bytes_read = is.read(buffer);
}
Log.e(TAG, aOutputBuffer.toString());
return aOutputBuffer.toString();
} else {
return "please input true cmd";
}
} catch (IOException e) {
e.printStackTrace();
return "operater err";
}
}
使用方式
ChangeBuildPropTools.exec("chmod -R 777" + EMAIL_SEAFILE_PATH);