Copying data between channels - Java File Path IO

Java examples for File Path IO:File Channel

Description

Copying data between channels

Demo Code

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;

public class Main {
  public static void main(String[] argv) throws IOException {
    ReadableByteChannel source = Channels.newChannel(System.in);
    WritableByteChannel dest = Channels.newChannel(System.out);

    channelCopy1(source, dest);/*from   w  ww. j a  v a2s  .  co  m*/

    source.close();
    dest.close();
  }

  private static void channelCopy1(ReadableByteChannel src,
      WritableByteChannel dest) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);

    while (src.read(buffer) != -1) {
      buffer.flip();

      dest.write(buffer);

      buffer.compact();
    }
    buffer.flip();
    while (buffer.hasRemaining()) {
      dest.write(buffer);
    }
  }

}

Related Tutorials