Skip to content

Instantly share code, notes, and snippets.

@Eugeny
Created April 23, 2011 14:22
Show Gist options
  • Save Eugeny/938641 to your computer and use it in GitHub Desktop.
Save Eugeny/938641 to your computer and use it in GitHub Desktop.
Writer w = SUFS.writeFile("/data/tempfile");
try {
w.stream.writeChars("A\nB\nC\n");
} catch (IOException e) {
e.printStackTrace();
}
w.finish();
public final class SUFS {
...
public static Writer writeFile(String file) {
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream sw = new DataOutputStream(p.getOutputStream());
sw.writeBytes("tee " + file + "\nexit\n");
sw.flush();
Writer w = new Writer();
w.proc = p;
w.stream = sw;
return w;
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static class Writer {
public DataOutputStream stream;
private Process proc;
public void finish() {
try {
stream.close();
proc.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment