2018-09-03
cat file1 > file2????//讀取/復制file1為file2
echo "string" > file? ? //覆蓋寫入string到file
echo "string" >> file? ? //追加寫入string到file
//注意引號里面的內(nèi)容string要replaceAll("\\n", "\\\\n")血公,replaceAll("\"", "\\\\\"");
下面是一個執(zhí)行多條命令的方法
public static CommandResultexecCommand(String[] commands, boolean isRoot) {
????????CommandResult commandResult =new CommandResult();
? ? ? ? if (commands ==null || commands.length ==0)return commandResult;
? ? ? ? Process process =null;
? ? ? ? DataOutputStream os =null;
? ? ? ? BufferedReader successResult =null;
? ? ? ? BufferedReader errorResult =null;
? ? ? ? StringBuilder successMsg =null;
? ? ? ? StringBuilder errorMsg =null;
? ? ? ? try {
????????????process = Runtime.getRuntime().exec(isRoot ?COMMAND_SU :COMMAND_SH);
? ? ? ? ? ? os =new DataOutputStream(process.getOutputStream());
? ? ? ? ? ? for (String command : commands) {
????????????????if (command !=null) {
????????????????os.write(command.getBytes());
? ? ? ? ? ? ? ? ? ? os.writeBytes(COMMAND_LINE_END);
? ? ? ? ? ? ? ? ? ? os.flush();
? ? ? ? ? ? ? ? }
????????}
????????os.writeBytes(COMMAND_EXIT);
? ? ? ? ? ? os.flush();
? ? ? ? ? ? commandResult.result = process.waitFor();
? ? ? ? ? ? //獲取錯誤信息
? ? ? ? ? ? successMsg =new StringBuilder();
? ? ? ? ? ? errorMsg =new StringBuilder();
? ? ? ? ? ? successResult =new BufferedReader(new InputStreamReader(process.getInputStream()));
? ? ? ? ? ? errorResult =new BufferedReader(new InputStreamReader(process.getErrorStream()));
? ? ? ? ? ? String s;
? ? ? ? ? ? while ((s = successResult.readLine()) !=null) successMsg.append(s);
? ? ? ? ? ? while ((s = errorResult.readLine()) !=null) errorMsg.append(s);
? ? ? ? ? ? commandResult.successMsg = successMsg.toString();
? ? ? ? ? ? commandResult.errorMsg = errorMsg.toString();
? ? ? ? }catch (IOException e) {
????????????String errmsg = e.getMessage();
? ? ? ? ? ? if (errmsg !=null) {
????????????????Log.e(TAG, errmsg);
? ? ? ? ? ? }else {
????????????????e.printStackTrace();
? ? ? ? ? ? }
????????}catch (Exception e) {
????????????String errmsg = e.getMessage();
? ? ? ? ? ? if (errmsg !=null) {
????????????????Log.e(TAG, errmsg);
? ? ? ? ? ? }else {
????????????????e.printStackTrace();
? ? ? ? ? ? }
????????}finally {
????????try {
????????????????if (os !=null) os.close();
? ? ? ? ? ? ? ? if (successResult !=null) successResult.close();
? ? ? ? ? ? ? ? if (errorResult !=null) errorResult.close();
? ? ? ? ? ? }catch (IOException e) {
????????????????String errmsg = e.getMessage();
? ? ? ? ? ? ? ? if (errmsg !=null) {
????????????????????Log.e(TAG, errmsg);
? ? ? ? ? ? ? ? }else {
????????????????????e.printStackTrace();
? ? ? ? ? ? ? ? }
????????}
????????if (process !=null) process.destroy();
?????}
?????return commandResult;
}
修改Packages.xml文件權(quán)限
String path ="/storage/emulated/0/temp.xml";
CommandExecution.execCommand("cat /data/system/packages.xml > " + path, true);
File file =new File(path);
if (!file.exists()) {
????Log.d("File", path +"不存在");
}else {
????StringBuffer sb =new StringBuffer();
? ? try {
????????InputStream is =new FileInputStream(file);
? ? ? ? if (is !=null) {
????????????InputStreamReader isr =new InputStreamReader(is);
? ? ? ? ? ? BufferedReader br =new BufferedReader(isr);
? ? ? ? ? ? String line;
? ? ? ? ? ? while ((line = br.readLine()) !=null) {
????????????????sb.append(line);
? ? ? ? ? ? ? ? sb.append("\n");
? ? ? ? ? ? }
????????????CommandExecution.execCommand("echo \"" +
????????????sb.toString().replaceAll("granted=\"false\"", "granted=\"true\"")
????????????????????????????????.replaceAll("\\n", "\\\\n")
????????????????????????????????.replaceAll("\"", "\\\\\"") +
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "\" > /data/system/packages.xml", true);
? ? ? ? ? ? Log.i("setPermission", "Success");
? ? ? ? }
}catch (FileNotFoundException e) {
????????Log.d("FileNotFoundException", file.getAbsolutePath() +" doesn't found\n" + e.getMessage());
? ? }catch (IOException e) {
????????Log.d("File", "read exception: " + e.getMessage());
? ? }
}