copy Asset File - Android App

Android examples for App:Assets File

Description

copy Asset File

Demo Code


import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.content.Context;

public class Main{
    static final String TAG = BIOUtilities.class.getSimpleName();
    /**/*from www .  ja va  2 s .  co m*/
     * 
     * @param ctx
     * @param srcFileName
     * @param dst
     */
    public static final void copyAssetFile(Context ctx, String srcFileName,
            File dst) {
        InputStream is = null;
        OutputStream os = null;
        try {
            if (!dst.exists()) {
                boolean result = dst.mkdirs();
                BLog.i(TAG, "make dir " + dst.getAbsolutePath()
                        + "; result:" + result);
            }
            is = ctx.getAssets().open(srcFileName);
            String[] filePath = srcFileName.split(File.separator);
            String fileName = filePath[filePath.length - 1];
            os = new FileOutputStream(new File(dst, fileName));
            byte[] b = new byte[1024];
            int len;
            while ((len = is.read(b)) != -1) {
                os.write(b, 0, len);
            }
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

Related Tutorials