user271280
user271280

Reputation: 49

How to copy a file in filesystem?

I would like to know the best way to copy a file in the filesystem? (android java function )

(sdcard/video/test.3gp -----> sdcard/video_bis/test2.3gp)

Is there an example somewhere?

Regards

Upvotes: 1

Views: 1831

Answers (4)

Diego Torres Milano
Diego Torres Milano

Reputation: 69198

You may use

private static final String[] COMMAND = { "dd", "if=/sdcard/video/test.3gp", "of=/sdcard/video_bis/test2.3gp", "bs=1024" };

// ...

try {
    final Process pr = Runtime.getRuntime().exec(COMMAND);
    final int retval = pr.waitFor();
    if ( retval != 0 ) {
        System.err.println("Error:" + retval);
    }
}
catch (Exception e) {
    // TODO: handle exception
}

Works on the emulator, you should check if it works on your phone.

Upvotes: 0

Mikael Ohlson
Mikael Ohlson

Reputation: 3264

I guess it depends on what you mean by best way of copying the file.

Since the file is on the sdcard you can use the normal java.io-package for reading and writing the file in the new place, as per Erich's answer.

Another option is accessing the shell, which I don't know if it will work, but which might be more efficient, since it uses the underlying system's cp-command.

In this case I assume that commands would only contain something like "cp /sdcard/video/test.3gp /sdcard/video_bis/test2.3gp".

Even if this does work, I expect that this might stop working, since it really seems like a security issue in ways..

Upvotes: 1

Erich Douglass
Erich Douglass

Reputation: 52002

You can copy the file using standard Java I/O streams - there's nothing special you need to do. Here's an example on copying a file. You might want to change the example so it's copying more than 1 byte at a time, though :)

Upvotes: 1

Related Questions