集册 Java实例教程 在通道之间复制数据

在通道之间复制数据

欢马劈雪     最近更新时间:2020-01-02 10:19:05

522
在通道之间复制数据

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.Channels;

import java.nio.channels.ReadableByteChannel;/**来 自 n  o  w  j  a  v  a . c o m**/

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);


    source.close();

    dest.close();
    /** 
     来自 n o w j a v a . c o m**/

  }


  private static void channelCopy1(ReadableByteChannel src,

      WritableByteChannel dest) throws IOException {

    ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);


    while (src
展开阅读全文