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.os.Environment;

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 final String DATA_DIR = Environment.getDataDirectory() + "/data/";

    public static void copyFileOrDir(Context ctx, String path) throws IOException {
        String assets[] = ctx.getApplicationContext().getAssets().list(path);
        if (assets.length == 0) {
            copyFile(ctx, path);
        } else {
            StringBuilder fullPath = new StringBuilder();
            fullPath.append(DATA_DIR).append(ctx.getPackageName()).append("/").append(path);
            File dir = new File(fullPath.toString());
            if (!dir.exists())
                dir.mkdir();
            for (int i = 0; i < assets.length; ++i) {
                copyFileOrDir(ctx, path + "/" + assets[i]);
            }
        }
    }

    private static void copyFile(Context ctx, String filename) throws IOException {
        InputStream in = null;
        OutputStream out = null;

        in = ctx.getApplicationContext().getAssets().open(filename);
        StringBuilder newFileName = new StringBuilder();
        newFileName.append(DATA_DIR).append(ctx.getPackageName()).append("/").append(filename);
        out = new FileOutputStream(newFileName.toString());

        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        in = null;
        out.flush();
        out.close();
        out = null;
    }
}