Android ByteArrayOutputStream Write copyTo(Context context, String fromPath, String toFile)

Here you can find the source of copyTo(Context context, String fromPath, String toFile)

Description

copy To

Declaration

public static void copyTo(Context context, String fromPath,
            String toFile) 

Method Source Code

//package com.java2s;
import android.content.*;
import java.io.*;

public class Main {
    public static void copyTo(Context context, String fromPath,
            String toFile) {/*w w w.  ja v  a2  s .  c om*/
        try {
            if (!new File(toFile).exists()) {
                new File(toFile).createNewFile();
            }
            InputStream fromFileIs = context.getResources().getAssets()
                    .open(fromPath);
            int length = fromFileIs.available();
            byte[] buffer = new byte[length];
            FileOutputStream fileOutputStream = new FileOutputStream(toFile);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(
                    fromFileIs);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                    fileOutputStream);
            int len = bufferedInputStream.read(buffer);
            while (len != -1) {
                bufferedOutputStream.write(buffer, 0, len);
                len = bufferedInputStream.read(buffer);
            }
            bufferedInputStream.close();
            bufferedOutputStream.close();
            fromFileIs.close();
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Related

  1. writeUint32BE(ByteArrayOutputStream buffer, long value)
  2. writeUint32LE(ByteArrayOutputStream buffer, long value)
  3. writeUint64BE(ByteArrayOutputStream buffer, long value)
  4. writeUint64LE(ByteArrayOutputStream buffer, long value)
  5. writeInt(ByteArrayOutputStream baos, int i)
  6. copyStream(ByteArrayOutputStream source, ByteArrayOutputStream target)