private static void nioCopy(FileOutputStream fos,FileInputStream fis) throws IOException {
  FileChannel outChannel=fos.getChannel();
  FileChannel inChannel=fis.getChannel();
  long length=inChannel.size();
  long offset=0;
  while (true) {
    long remaining=length - offset;
    long toTransfer=remaining < MAX_TRANSFER_SIZE ? remaining : MAX_TRANSFER_SIZE;
    long transferredBytes=inChannel.transferTo(offset,toTransfer,outChannel);
    offset+=transferredBytes;
    length=inChannel.size();
    if (offset >= length) {
      break;
    }
  }
}

private static void nioCopy ( FileOutputStream fos , FileInputStream fis ) throws IOException { FileChannel outChannel = fos . getChannel ( ) ; FileChannel inChannel = fis . getChannel ( ) ; long length = inChannel . size ( ) ; long offset = 0 ; while ( true ) { long remaining = length - offset ; long toTransfer = remaining < MAX_TRANSFER_SIZE ? remaining : MAX_TRANSFER_SIZE ; long transferredBytes = inChannel . transferTo ( offset , toTransfer , outChannel ) ; offset += transferredBytes ; length = inChannel . size ( ) ; if ( offset >= length ) { break ; } } }
copy a single file using nio .
