Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static boolean copyAssetFile(Context context, String originFileName, String destFilePath,
            String destFileName) {
        InputStream is = null;
        BufferedOutputStream bos = null;
        try {
            is = context.getAssets().open(originFileName);
            File destPathFile = new File(destFilePath);
            if (!destPathFile.exists()) {
                destPathFile.mkdirs();
            }

            File destFile = new File(destFilePath + File.separator + destFileName);
            if (!destFile.exists()) {
                destFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(destFile);
            bos = new BufferedOutputStream(fos);

            byte[] buffer = new byte[256];
            int length = 0;
            while ((length = is.read(buffer)) > 0) {
                bos.write(buffer, 0, length);
            }
            bos.flush();

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }
}