android在根目录中打开文件
是否有任何方法可以在根目录(例如/data/)中写入和读取根安卓手机上的文本文件android在根目录中打开文件,android,root,Android,Root,是否有任何方法可以在根目录(例如/data/)中写入和读取根安卓手机上的文本文件 不起作用要执行您要求的操作,您必须通过SU二进制文件执行所有操作 像 try { Process process = Runtime.getRuntime().exec("su"); process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedExcep
不起作用要执行您要求的操作,您必须通过SU二进制文件执行所有操作 像
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
读比写容易,因为写最容易的方法是使用标准java api将文件写入您可以访问的某个位置,然后使用su二进制文件将其移动到新位置。为了能够执行您要求的操作,您必须通过su二进制文件执行所有操作 像
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
读比写容易,因为写最简单的方法是使用标准java api将文件写入您可以访问的某个位置,然后使用su二进制文件将其移动到新位置。您只能访问/data文件夹,因为您是根用户 通过OutputStream调用SU binary并通过SU binary写入字节(这些字节就是命令),通过InputStream读取命令输出,很容易:
调用
cat
命令读取文件
try {
Process process = Runtime.getRuntime().exec("su");
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();
String cmd = "cat /data/someFile";
out.write(cmd.getBytes());
out.flush();
out.close();
byte[] buffer = new byte[1024 * 12]; //Able to read up to 12 KB (12288 bytes)
int length = in.read(buffer);
String content = new String(buffer, 0, length);
//Wait until reading finishes
process.waitFor();
//Do your stuff here with "content" string
//The "content" String has the content of /data/someFile
} catch (IOException e) {
Log.e(TAG, "IOException, " + e.getMessage());
} catch (InterruptedException e) {
Log.e(TAG, "InterruptedException, " + e.getMessage());
}
不要将OutputStream用于写入文件,
OutputStream
用于在SU二进制文件中执行命令,InputStream
用于获取命令的输出。您只能访问/data文件夹,因为您是根用户
通过OutputStream调用SU binary并通过SU binary写入字节(这些字节就是命令),通过InputStream读取命令输出,很容易:调用
cat
命令读取文件
try {
Process process = Runtime.getRuntime().exec("su");
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();
String cmd = "cat /data/someFile";
out.write(cmd.getBytes());
out.flush();
out.close();
byte[] buffer = new byte[1024 * 12]; //Able to read up to 12 KB (12288 bytes)
int length = in.read(buffer);
String content = new String(buffer, 0, length);
//Wait until reading finishes
process.waitFor();
//Do your stuff here with "content" string
//The "content" String has the content of /data/someFile
} catch (IOException e) {
Log.e(TAG, "IOException, " + e.getMessage());
} catch (InterruptedException e) {
Log.e(TAG, "InterruptedException, " + e.getMessage());
}
不要将OutputStream用于写入文件,OutputStream
用于在SU二进制文件中执行命令,InputStream
用于获取命令的输出