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.nio.channels.*;

import java.text.*;

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

    public static void fastFileCopy(File source, File target) {
        FileChannel in = null;
        FileChannel out = null;
        long start = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            in = fis.getChannel();
            fos = new FileOutputStream(target);
            out = fos.getChannel();

            long size = in.size();
            long transferred = in.transferTo(0, size, out);

            while (transferred != size) {
                transferred += in.transferTo(transferred, size - transferred, out);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(fis);
            close(fos);
            close(in);
            close(out);
        }
        long end = System.currentTimeMillis();

        Log.d(target.getAbsolutePath(), nf.format(source.length()) + "B: "
                + ((end - start > 0) ? nf.format(source.length() / (end - start)) : 0) + " KB/s");
    }

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