Java NIO FileChannel versus FileOutputstream rendimiento / utilidad

Estoy tratando de averiguar si hay alguna diferencia en el rendimiento (o ventajas) cuando usamos nioFileChannel versus normalFileInputStream/FileOuputStream para leer y escribir archivos al sistema de archivos. Observé que en mi máquina ambos funcionan al mismo nivel, también muchas veces elFileChannel El camino es más lento. ¿Puedo saber más detalles comparando estos dos métodos? Aquí está el código que usé, el archivo con el que estoy probando está alrededor350MB. ¿Es una buena opción usar clases basadas en NIO para la E / S de archivos, si no estoy viendo el acceso aleatorio u otras características avanzadas?

package trialjavaprograms;

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

public class JavaNIOTest {
    public static void main(String[] args) throws Exception {
        useNormalIO();
        useFileChannel();
    }

    private static void useNormalIO() throws Exception {
        File file = new File("/home/developer/test.iso");
        File oFile = new File("/home/developer/test2");

        long time1 = System.currentTimeMillis();
        InputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(oFile);
        byte[] buf = new byte[64 * 1024];
        int len = 0;
        while((len = is.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        fos.flush();
        fos.close();
        is.close();
        long time2 = System.currentTimeMillis();
        System.out.println("Time taken: "+(time2-time1)+" ms");
    }

    private static void useFileChannel() throws Exception {
        File file = new File("/home/developer/test.iso");
        File oFile = new File("/home/developer/test2");

        long time1 = System.currentTimeMillis();
        FileInputStream is = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(oFile);
        FileChannel f = is.getChannel();
        FileChannel f2 = fos.getChannel();

        ByteBuffer buf = ByteBuffer.allocateDirect(64 * 1024);
        long len = 0;
        while((len = f.read(buf)) != -1) {
            buf.flip();
            f2.write(buf);
            buf.clear();
        }

        f2.close();
        f.close();

        long time2 = System.currentTimeMillis();
        System.out.println("Time taken: "+(time2-time1)+" ms");
    }
}

Respuestas a la pregunta(7)

Su respuesta a la pregunta