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.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    private static boolean CopyFile(Context context, String pathName) {

        boolean isCopyCompleted = false;

        InputStream inputStream = null;
        OutputStream outputStream = null;

        try {
            inputStream = context.getResources().getAssets().open(pathName);
            File outFile = new File(context.getCacheDir(), pathName);
            if (!outFile.exists()) {
                outFile.createNewFile();
            }

            outputStream = new FileOutputStream(outFile);

            byte[] buffer = new byte[4096];
            int bytesRead;
            // read from is to buffer
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
                Log.v("", "data..." + bytesRead);
            }

            isCopyCompleted = true;
            inputStream.close();
            // flush OutputStream to write any buffered data to file
            outputStream.flush();
            outputStream.close();

        } catch (IOException e) {
            isCopyCompleted = false;
            e.printStackTrace();
        }
        return isCopyCompleted;

    }
}