copy file from source path to target path - Android java.io

Android examples for java.io:File Copy

Description

copy file from source path to target path

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileOutputStream;

public class Main {
    public static String copyfile(String fromPath, String toPath,
            Boolean rewrite) {/*from  www.j  a va2 s .c  o m*/
        File fromFile = new File(fromPath);
        File toFile = new File(toPath);
        if (!fromFile.exists()) {
            return null;
        }
        if (!fromFile.isFile()) {
            return null;
        }
        if (!fromFile.canRead()) {
            return null;
        }
        if (!toFile.getParentFile().exists()) {
            toFile.getParentFile().mkdirs();
        }
        if (toFile.exists() && rewrite) {
            toFile.delete();
        }
        try {
            java.io.FileInputStream fosfrom = new java.io.FileInputStream(
                    fromFile);
            java.io.FileOutputStream fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = fosfrom.read(bt)) > 0) {
                fosto.write(bt, 0, c); // ??????????
            }
            fosfrom.close();
            fosto.close();
        } catch (Exception ex) {
            // Log.e("readfile", ex.getMessage());
        }
        return toPath;
    }
}

Related Tutorials