Netty--NIO的基本介绍(NIO组件,ByteBuffer基本使用,FileChannel的用法)

1. NIO 的三大组件

NIO从字面上理解:non-blocking io 非阻塞 IO

组件1:Channel是读写数据的双向通道

常见的 Channel:FileChannel、DatagramChannel、SocketChannel、ServerSocketChannel

组件2:Buffer用来缓冲读写数据,常见的 buffer 有ByteBuffer 

组件3:选择器

2. NIO操作文件相关的基本操作(FileChannel和ByteBuffer 配合使用)

FileChannel可以使用文本输入输出流创建,也可以用随机流创建

Bytebuffer模型:

创建一个普通的文本:

D:\\nettytest\\test.txt

内容:aabbcceeff

public class ByteBufferUtil {
    public static void main(String[] args) throws IOException {
        // 源文件地址
        FileChannel fileChannel = new RandomAccessFile("D:\\nettytest\\test.txt", "r").getChannel();
        // 目标文件地址
        FileChannel fileChannelTo = new RandomAccessFile("D:\\nettytest\\test1.txt", "rw").getChannel();
        // 设置定长的缓存区,分配空间
        ByteBuffer buffer = ByteBuffer.allocate(4);
        int len = -1;
        // 向 buffer 写入数据,读fileChannel到末尾的时候fileChannel.read(buffer))为-1
        while ((len = fileChannel.read(buffer)) != -1){
            buffer.flip();  // 切换到读模式(limit--最大限度 = position--指针;position = 0;)
            fileChannelTo.write(buffer);
            for (long i = 0; i < buffer.position(); i++) {
                System.out.println((char) buffer.get((int) i));
            }
            System.out.println("=================");
            // 切换到写模式( position = 0;limit = capacity--容量)
            buffer.clear();
        }
    }
}

控制台打印信息:

=================
a
a
b
b
=================
c
c
e
e
=================
f
f

3. transferTo()方法

需要复制大文件时,这个方法超级好用(底层限制,一次性最大复制2G,大于2G需要循环使用)

需要自己做文件分片操作的,可以看下这个博客:Java文件分块读写,文件分片读写(大文件分块写入本地并合入)_傻鱼爱编程的博客-CSDN博客_java 文件分片读取

 基本用法:

public class TestFileChannelTransferTo {
    public static void main(String[] args) {
        try (
                FileChannel from = new FileInputStream("D:\\nettytest\\666.zip").getChannel();
                FileChannel to = new FileOutputStream("D:\\nettytest\\888.zip").getChannel();
        ) {
            // 效率高,底层会利用操作系统的零拷贝进行优化, 最大2g数据
            long size = from.size();
            // left 变量代表还剩余多少字节,如果大于2g会循环多次
            for (long left = size; left > 0; ) {
                System.out.println("position:" + (size - left) + " left:" + left);
                /**
                 * 从from复制到to
                 * 参数1:(size - left)  复制起始点
                 * 参数2:(size - left)  复制末尾点
                 * 参数3:复制到哪个文件
                 */
                left -= from.transferTo((size - left), left, to);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值