A S A S - 1 year ago 75
Java Question

How to make a copy of a file in android?

In my app I want to save a copy of a certain file with a different name (which I get from user)

Do I really need to open the contents of the file and write it to another file?

What is the best way to do so?

Answer Source

To copy a file and save it to your destination path you can use the method below.

public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download