How do I use Java NIO to copy file?

This program is an example how we use the New IO API in Java programming. The API is in the java.nio.* package. It requires at least Java 1.4 because the API was first included in this version. The java.nio is a block based IO processing, instead of a stream based IO which is the old version IO processing in Java.

package org.kodejava.example.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CopyFileExample {
    public static void main(String[] args) throws Exception {
        String source = "medical-report.txt";
        String destination = "medical-report-final.txt";

        FileInputStream fis = new FileInputStream(source);
        FileOutputStream fos = new FileOutputStream(destination);

        FileChannel fci = fis.getChannel();
        FileChannel fco = fos.getChannel();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while (true) {
            int read = fci.read(buffer);
            if (read == -1) {
                break;
            }
            buffer.flip();
            fco.write(buffer);
            buffer.clear();
        }
    }
}