NIO中让人混淆的读通道写通道和buf.flip的记忆方法

2 篇文章 0 订阅

最近同学给我说NIO中读和写和开始学IO感觉一样,一会儿InputString,一会OutputString,经常搞不清楚这个输入和输出到底哪个对应哪个。我自己想了一个记这个的方法,分享给刚学NIO的同学,可以让你记得非常清楚。

先贴一段别人的程序:

public void test1(){//10874-10953
        long start = System.currentTimeMillis();

        FileInputStream fis = null;
        FileOutputStream fos = null;
        //①获取通道
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            fis = new FileInputStream("d:/1.mkv");
            fos = new FileOutputStream("d:/2.mkv");

            inChannel = fis.getChannel();
            outChannel = fos.getChannel();

            //②分配指定大小的缓冲区
            ByteBuffer buf = ByteBuffer.allocate(1024);

            //③将通道中的数据存入缓冲区中
            while(inChannel.read(buf) != -1){
                buf.flip(); //切换读取数据的模式
                //④将缓冲区中的数据写入通道中
                outChannel.write(buf);
                buf.clear(); //清空缓冲区
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(outChannel != null){
                try {
                    outChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(inChannel != null){
                try {
                    inChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        long end = System.currentTimeMillis();

上面的 inChannel.read(buf),如何记才不容易混淆,按我的方法来:
inChannel调用的read方法
将 inChannel.read(buf) 反着看 buf read inChannel,直译过来:缓冲区读通道,就是缓冲区读通道的另一端,在上面代码块中就是读文件。
同样的,outChannel.write(buf)反过来看,buf write outChannel:缓冲区写入通道,就是把缓冲区写到通道的另一端,在上面的代码块中就是写文件。

这样记就非常好记了,尤其是刚学IO/NIO 读入输入让人很容易记混淆。

然后还有一个混淆的就是 buf.flip(); //切换读取数据的模式
这是buf调用的flip对吧,我觉得应该flip应该叫切换到buf数据被读模式,其实就是把buf里的数据输出的开关打开了。既然大家都把调用flip叫读模式,那我就把flip记成
同样的反过来看 flip buf:读缓冲区。 这样就代表,buf现在被通道读的开关打开了。这样记就轻松多了,分享给同学,同学觉得非常好记。于是分享给大家~~

NIO通道方法包括: 1. 打开通道: - `FileChannel.open(Path path, OpenOption... options)`:打开文件通道。 - `SocketChannel.open()`:打开套接字通道。 - `ServerSocketChannel.open()`:打开服务器套接字通道。 - `DatagramChannel.open()`:打开数据报通道。 2. 取数据: - `int read(ByteBuffer dst)`:从通道取数据到缓冲区。 - `long read(ByteBuffer[] dsts, int offset, int length)`:从通道批量取数据到多个缓冲区。 3. 入数据: - `int write(ByteBuffer src)`:将缓冲区的数据通道。 - `long write(ByteBuffer[] srcs, int offset, int length)`:将多个缓冲区的数据批量通道。 4. 关闭通道: - `void close()`:关闭通道。 下面是一个简单的示例代码,演示了如何使用NIO通道进行文件操作: ```java import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.StandardOpenOption; public class ChannelExample { public static void main(String[] args) { try (FileChannel channel = FileChannel.open(Path.of("input.txt"), StandardOpenOption.READ)) { ByteBuffer buffer = ByteBuffer.allocate(1024); // 从通道取数据到缓冲区 int bytesRead = channel.read(buffer); while (bytesRead != -1) { buffer.flip(); // 切换缓冲区为模式 while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); // 处理数据 } buffer.clear(); // 清空缓冲区 bytesRead = channel.read(buffer); } } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码通过打开一个文件通道,从文件取数据并打印到控制台。首先创建一个ByteBuffer作为缓冲区,然后循环取数据到缓冲区,通过flip()方法切换缓冲区为模式,处理数据后再清空缓冲区。最后关闭通道
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值