Reputation: 508
Is there any way to write and read text files on rooted Android phone in root directories (ex. /data/)?
InputStream instream = openFileInput("/data/somefile");
doesn't work
Upvotes: 0
Views: 2969
Reputation:
You can only access to /data folder is you're root user.
Call to SU binary and write bytes (these bytes are the command) over SU binary via OutputStream and read command output via InputStream, it's easy:
Call to cat
command to read files.
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());
}
Don't use OutputStream for write files, OutputStream
is used for execute commands inside SU binary, and InputStream
is used for get output of command.
Upvotes: 3
Reputation: 8325
To be able to do what you are asking you must do all of the operations via the SU binary.
like...
try {
Process process = Runtime.getRuntime().exec("su");
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
Reading will be easier then writing, for writing easiest would be to write the file to some place where u have access using standard java api's and then move it to the new location using the su binary.
Upvotes: 1