• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Writing to a file using shell command through apk

abhirocks

Lurker
Hi,

I would like to write to a file using shell command (eg: ls /data >> test.txt)...
Below is the code snippet I am using.
I am able to execute shell commands using this snippet but unable to write to any file using shell command...
There are other ways to write the file using android built in functions but my requirement is to write to a file through a shell command.
Can anyone please help me out on this...

Code snippet:

@Override​
protected void onStart() {
super.onStart();

Handler handler1 =
new Handler();
handler1.postDelayed(
new Runnable() {
public void run() {
String cmd = "ls /data >> /sdcard/date.txt";
java.lang.Process p;
try {
p = Runtime.getRuntime().exec(cmd);
BufferedReader input =
new BufferedReader(new InputStreamReader(p.getInputStream()),cmd.length());
Log.i(
"AbhiRocks", "Output is " + input.readLine());
}
catch (IOException e) {

e.printStackTrace();
}

}
}, 5000);

 
}


I have added the below line to manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


Thanks in advance
 
The command in ls /data is read only... U can't write to it....
But lets chuck out ls /data....
Can you please help me out if its 'ls /sdcard' instead of 'ls /data' in the code.
I just need to write to a file on the sdcard by creating a file through apk which calls the command line
 
Back
Top Bottom