package io.nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Author frank
 * @Date 17-9-19
 *
 * NIO
 * 非阻塞io
 * 基于通道和缓冲区的
 *
 */
public class Main {

    public static final String FILE_PATH = "/home/frank/log/test";

    public static void main(String[] args) throws IOException {

        RandomAccessFile file = new RandomAccessFile(FILE_PATH, "rw");

        FileChannel channel = file.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(48);

        int read = channel.read(byteBuffer);
        while (read != -1) {
            byteBuffer.flip();

            while (byteBuffer.hasRemaining()) {
                System.out.print((char)byteBuffer.get());
            }

            byteBuffer.compact();
            read = channel.read(byteBuffer);
        }
        file.close();
    }

    public static void foo() {



    }
}