Android File Copy CopyFile(Context con, String assetsFile)

Here you can find the source of CopyFile(Context con, String assetsFile)

Description

Copy File

Declaration

public static String CopyFile(Context con, String assetsFile) 

Method Source Code

//package com.java2s;
import android.content.*;
import java.io.*;

public class Main {
    public static String CopyFile(Context con, String assetsFile) {
        String out = getDataFilesDir(con) + assetsFile;
        copyTo(con, assetsFile, out);/*  w  w  w  .  j  av  a  2 s.  c o m*/
        File f = new File(out);
        if (!f.exists()) {
            return null;
        }
        return out;
    }

    public static String getDataFilesDir(Context c) {
        return c.getFilesDir().getAbsolutePath() + File.separator;
    }

    public static void copyTo(Context context, String fromPath,
            String toFile) {
        try {
            if (!new File(toFile).exists()) {
                new File(toFile).createNewFile();
            }
            InputStream fromFileIs = context.getResources().getAssets()
                    .open(fromPath);
            int length = fromFileIs.available();
            byte[] buffer = new byte[length];
            FileOutputStream fileOutputStream = new FileOutputStream(toFile);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(
                    fromFileIs);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    fileOutputStream);
            int len = bufferedInputStream.read(buffer);
            while (len != -1) {
                bufferedOutputStream.write(buffer, 0, len);
                len = bufferedInputStream.read(buffer);
            }
            bufferedInputStream.close();
            bufferedOutputStream.close();
            fromFileIs.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Related

  1. fileCopy(String sourcefile, String destinctionFile)
  2. bakFile(String source, String dest)
  3. copyFile(File aSourceFile, File aTargetFile, boolean aAppend)
  4. copyFile(File from, File to)
  5. CopyCacheFile(Context con, String assetsFile)
  6. copyFile(File from, File to)
  7. copyFile(File from, File to)
  8. copyFile(File sourceFile, File targetFile)
  9. copyFile(String source, String dest)