Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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 void copyDatabase2FileDir(Context context, String dbName) {
        InputStream stream = null;
        BufferedOutputStream outputStream = null;
        try {

            stream = context.getAssets().open(dbName);
            File file = new File(context.getFilesDir(), dbName);

            outputStream = new BufferedOutputStream(new FileOutputStream(file));
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = stream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
                outputStream.flush();
            }

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

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