Android File Copy CopyCacheFile(Context con, String assetsFile)

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

Description

Copy Cache File

Declaration

public static String CopyCacheFile(Context con, String assetsFile) 

Method Source Code

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

public class Main {
    public static String CopyCacheFile(Context con, String assetsFile) {
        String out = getSdcardFilesDir(con) + assetsFile;
        copyTo(con, assetsFile, out);//from www. ja v  a 2  s.  c  om
        File f = new File(out);
        if (!f.exists()) {
            return null;
        }
        return out;
    }

    public static String getSdcardFilesDir(Context c) {
        return c.getExternalFilesDir(null).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(File sourcefile, String destinctionFile)
  2. fileCopy(String sourcefile, String destinctionFile)
  3. bakFile(String source, String dest)
  4. copyFile(File aSourceFile, File aTargetFile, boolean aAppend)
  5. copyFile(File from, File to)
  6. CopyFile(Context con, String assetsFile)
  7. copyFile(File from, File to)
  8. copyFile(File from, File to)
  9. copyFile(File sourceFile, File targetFile)