集册 Java实例教程 使用ByteBuffer从通道读取

使用ByteBuffer从通道读取

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

551
使用ByteBuffer从通道读取

import java.io.FileInputStream;

import java.nio.ByteBuffer;

import java.nio.channels.ReadableByteChannel;
/* from NowJava.com - 时代Java*/

public class Main {

  public static void main(String[] argv) {

    try {

      // Obtain a channel

      ReadableByteChannel channel = new FileInputStream("infile").getChannel();


      // Create a direct ByteBuffer; see also Creating a ByteBuffer

      ByteBuffer buf = ByteBuffer.allocateDirect(10);


      int numRead = 0;/*NowJava.com - 时  代  Java 提 供*/

      while (numRead >= 0) {

        buf.rewind();


        // Read bytes from the channel

        numRead = channel.read(buf);


        buf.rewind();


        for 
展开阅读全文