I currently need to issue system commands from my java code but I stuck for a long time. I have been trying many forums and many code snippets but all either crash the app or seem to do nothing The last code I tried though works with "echo" but when I make "echo anystuff > y.txt", instead of creating a file called y.txt and write "anystuff" in the file, it just shows "anystuff > y.txt" in the textbox I created to see the output here is my method
public void ShellTest() throws IOException {
// test 10
String cmd="echo anystuff > y.txt";
StringBuffer cmdOut = new StringBuffer();
Process process;
try{
// issue the command here
process = Runtime.getRuntime().exec(cmd);
// prepare to get back the result and put it in the textbox "resText"
DataOutputStream stdin = new DataOutputStream(process.getOutputStream());
stdin.writeBytes(cmd);
InputStreamReader r = new InputStreamReader(process.getInputStream());
BufferedReader bufReader = new BufferedReader(r);
char[] buf = new char[4096];
int nRead = 0;
while ((nRead = bufReader.read(buf)) > 0){
cmdOut.append(buf,0,nRead);
}
bufReader.close();
try {
process.waitFor();
} catch (InterruptedException e){
e.printStackTrace();
}
} catch (IOException e){
e.printStackTrace();
}
// check by the showing the output of the command in the textbox
resText.setText(cmdOut);
}
BTW my phone is Google Pixel 2 XL
Any help would be appreciated!