3

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?

imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
Rohith Nandakumar
  • 11,367
  • 11
  • 50
  • 60

1 Answers1

1

As noted in comment, you need a shell to do process output redirect (via the >).

You can simply append files via this code:

void append(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst, true);  // `true` means append 

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
       out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

In your case call it twice for file1 and file2.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154
  • thanks. I was making it too complex :D Also, is there some way to merge two video files? – Rohith Nandakumar Oct 14 '11 at 11:43
  • Merging video files is an entirely different problem. First they need to be unpacked from container (.avi or .mp4), then video streams need to be decoded, then appended into one, then encoded and then packed into container. This is a job for a video encoding/decoding library (e.g. ffmpeg). Android does not come with such functionality. – Peter Knego Oct 14 '11 at 11:50
  • Btw, if answer sufficiently answers your question, then you can accept it, right? – Peter Knego Oct 14 '11 at 12:20
  • it didn't work for video files , it just played the first chunked of the video file – Anshuman Pattnaik Jun 15 '16 at 16:51
  • there is a little error, you are putting `true` in the `FileInputStream` constructor but it should go to the `FileOutputStream` where you actually want to append – Asiel Diaz Benitez May 03 '21 at 06:32