4

I read one article regarding trasfer copy at https://www.ibm.com/developerworks/library/j-zerocopy/. which suggest to user channel for IO operation.

There is a banchmark of copy file operation available at https://baptiste-wicht.com/posts/2010/08/file-copy-in-java-benchmark.html

As per benchmark I can use either nio buffer or nio trasfer

I also read FileChannel does buffereing in the OS level here How to implement a buffered / batched FileChannel in Java?

Which is more efficient way to copy file with buffer or with out buffer.

code for nio buffer

public static void nioBufferCopy(File sourceFile, File targetFile, int BUFFER) {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(sourceFile).getChannel();
            outputChannel = new FileOutputStream(targetFile).getChannel();
            ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER);
            while (inputChannel.read(buffer) != -1) {
                buffer.flip();          
                while(buffer.hasRemaining()){
                    outputChannel.write(buffer);
                }           
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
           //close resource
        }
}

code for nio trasfer

public void copyFileWithChannels(File aSourceFile, File aTargetFile) {              
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        FileInputStream inStream = null;
        FileOutputStream outStream = null;      
        try {
            inStream = new FileInputStream(aSourceFile);
            inChannel = inStream.getChannel();
            outStream = new  FileOutputStream(aTargetFile);        
            outChannel = outStream.getChannel();
            long bytesTransferred = 0;
            while(bytesTransferred < inChannel.size()){
                bytesTransferred += inChannel.transferTo(bytesTransferred, inChannel.size(), outChannel);
            }
        }       
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            //close resource
        }              
}
mcacorner
  • 1,304
  • 3
  • 22
  • 45
  • 2
    `FileChannel` does not do buffering in the OS, beyond what the OS does for any open file. Your first code isn't optimal, but. the transfer APIs are stated to be the most efficient, – user207421 May 24 '17 at 18:10
  • 3
    if you want really really efficient file copy then java is the wrong tool for the job. on unixes you'll want to use use reflink copies where available and probe for holes in sparse files otherwise. And on linux the splice + pipe trick can avoid copying to userspace. But none of those things are exposed in the java standard library. Well, with some JNA or JNR glue it would be possible in java of course. – the8472 Jun 01 '17 at 12:44

1 Answers1

3

This question was asked before:

Java NIO FileChannel versus FileOutputstream performance / usefulness

TL.DR.: It matters what your JVM is running on, but mostly the java.nio is slightly faster.

Hash
  • 4,647
  • 5
  • 21
  • 39