I want to concatenate two files in android. I did this from the Terminal Emulator app using the command cat file1 file2 > output_file
. But it does not work when I try to execute it from my code.
This is the code that I used to execute the command.
public String exec() {
try {
// Executes the command.
String CAT_COMMAND = "/system/bin/cat /sdcard/file1 /sdcard/file2 > /sdcard/output_file";
Process process = Runtime.getRuntime().exec(CAT_COMMAND);
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
I have given the permission to write to external storage in the manifest. What am I missing?