1

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

user271280
  • 49
  • 4

4 Answers4

2

Standard concise way to copy a file in Java?

Community
  • 1
  • 1
Luke Dunstan
  • 4,862
  • 1
  • 17
  • 15
1

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 :)

Erich Douglass
  • 51,744
  • 11
  • 75
  • 60
1

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..

Mikael Ohlson
  • 3,074
  • 1
  • 22
  • 28
  • This makes more sense for what I *thought* you were asking about first, i.e. moving files. Perhaps that will teach me to think before answering. – Mikael Ohlson Feb 11 '10 at 19:15
0

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.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134