NIO

简单实例

    public static void main(String[] argv) throws Exception {
        // 获取Channel
        FileInputStream fin = new FileInputStream( "E:\\study\\HelloWorld\\src\\NIO\\Main.java" );
        FileChannel fc = fin.getChannel();
        // 从Channel读数据到Buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        fc.read(byteBuffer);
        // 将Buffer从读数据状态转换到输出数据状态
        byteBuffer.flip();
        // 从Buffer中读数据
        while (byteBuffer.remaining()>0) {
            byte b = byteBuffer.get();
            System.out.print(((char)b) );
        }
    }

Buffer读写图解

 

posted @ 2018-08-07 17:41  逃跑旅馆  阅读(87)  评论(0编辑  收藏  举报