Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.content.res.AssetManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static String copyAsset(Context context, String assetName, File dir) {
        try {
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File outFile = new File(dir, assetName);
            if (!outFile.exists()) {
                AssetManager assetManager = context.getAssets();
                InputStream in = assetManager.open(assetName);
                OutputStream out = new FileOutputStream(outFile);
                copyFile(in, out);
                in.close();
                out.close();
            }
            return outFile.getAbsolutePath();
        } catch (Exception e) {

        }
        return "";
    }

    private static void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }
}