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.util.*;
import java.io.*;

import java.text.*;

public class Main {
    static NumberFormat nf = NumberFormat.getInstance();

    public static void fastBufferFileCopy(File source, File target) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        long start = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        long size = source.length();
        try {
            fis = new FileInputStream(source);
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream(target);
            bos = new BufferedOutputStream(fos);

            byte[] barr = new byte[Math.min((int) size, 1 << 20)];
            int read = 0;
            while ((read = bis.read(barr)) != -1) {
                bos.write(barr, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(fis);
            close(fos);
            close(bis);
            close(bos);
        }
        long end = System.currentTimeMillis();

        String srcPath = source.getAbsolutePath();
        Log.d("Copied " + srcPath + " to " + target,
                ", took " + (end - start) / 1000 + " ms, total size " + nf.format(size) + " Bytes, speed "
                        + ((end - start > 0) ? nf.format(size / (end - start)) : 0) + "KB/s");
    }

    public static void close(Closeable closable) {
        if (closable != null) {
            try {
                closable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}