(三)Netty之Channel通道

通道(Channel)基本介绍

  1. NIO 的通道类似于流,但有些区别:
  • 通道可以同时进行读写,而流只能读或者只能写
  • 通道可以实现异步读写数据
  • 通道可以从缓冲读数据,也可以写数据到缓冲
    在这里插入图片描述
  1. BIO中的stream是单向的,例如FileInputStream对象只能进行读取数据的操作,而NIO中的通道(Channel)是双向的,可以读操作,也可以写操作
  2. Channel在NIO中是一个接口
    public interface Channel extends Closeable{}
  3. 常见的Channel类有:FileChannel、DatagramChannel、ServerSocketChannel和SocketChannel
  4. FileChannel用于文件的数据读写
    DatagramChannel用于UDP的数据读写
    ServerSocketChannel和SocketChannel用于TCP的数据读写
    在这里插入图片描述

FileChannel类

FileChannel主要用来对本地文件进行IO操作,常见的方法有

  1. public abstract int read(ByteBuffer dst) ,从通道读取数据并放到缓冲区重
  2. public abstract int write(ByteBuffer src),把缓冲区的数据写到通道中
  3. public abstract long transferFrom(ReadableByteChannel src, long position, long count),从目标通道中复制数据到当前通道
  4. public abstract long transferTo(long position, long count,WritableByteChannel target),把数据从当前通道复制给目标通道

应用实例1-本地文件写数据

使用前面学到的ByteBuffer(缓冲)和(通道),将字符串写入file01.txt中

package com.example.demo.netty;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 应用实例1-本地文件写数据
 * 使用前面学到的ByteBuffer(缓冲)和(通道),将字符串写入file01.txt中
 */
public class NioFileChannel01 {
    public static void main(String[] args) throws Exception {
        String str = "hello 世界";
        // 创建一个输出流 --> channel
        FileOutputStream fos = new FileOutputStream("D:/file01.txt");
        // 通过FileOutputStream 获取 对应的channel
        // 这个fileChannel 真实的类型是 FileChannelImpl
        FileChannel fileChannel = fos.getChannel();
        // 创建一个缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 将str放入缓冲区
        byteBuffer.put(str.getBytes());
        // 对byteBuffer进行反转
        byteBuffer.flip();
        // 将缓冲区数据写入fileChannel
        fileChannel.write(byteBuffer);
        fos.close();
    }
}

应用实例2-本地文件读数据

使用前面学到的ByteBuffer(缓冲)和(通道),将file01.txt中的数据读入到程序,并显示在控制台屏幕

package com.example.demo.netty;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 应用实例2-本地文件读数据
 * 使用前面学到的ByteBuffer(缓冲)和(通道),将file01.txt中的数据读入到程序,并显示在控制台屏幕
 */
public class NioFileChannel02 {
    public static void main(String[] args) throws Exception {
        // 创建文件的输入流
        FileInputStream fis = new FileInputStream("D:/file01.txt");
        // 通过FileInputStream 获取对应的 FileChannel -> 实际类型 FileChannelImpl
        FileChannel fileChannel = fis.getChannel();
        // 创建缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
        // 将通道的数据读取到buffer
        fileChannel.read(byteBuffer);
        // 将byteBuffer的 字节数据 转换成 string
        System.out.println(new String(byteBuffer.array()));
        fis.close();
    }
}

应用实例3-使用一个Buffer完成文件的读取

使用FileChannel(通道)和方法read write,完成文件的拷贝

package com.example.demo.netty;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 应用实例3-使用一个Buffer完成文件读取
 * 使用FileChannel(通道)和方法read write,完成文件的拷贝
 */
public class NioFileChannel03 {
    public static void main(String[] args) throws Exception {
        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel fileChannel01 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel fileChannel02 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        //循环读取
        while (true) {
            //一定不要忘了复位
            byteBuffer.clear();
            int read = fileChannel01.read(byteBuffer);
            if (read == -1) {//表示读完
                break;
            }
            //将buffer中的数据写入到fileChannel02 -> 2.txt
            byteBuffer.flip();
            fileChannel02.write(byteBuffer);
        }
    }
}

应用实例4-拷贝文件

使用FileChannel(通道)和方法transferFrom,完成文件的拷贝

package com.example.demo.netty;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

/**
 * 应用实例4-拷贝文件transferFrom方法
 * 使用FileChannel(通道)和方法transferFrom,完成文件的拷贝
 */
public class NioFileChannel04 {
    public static void main(String[] args) throws Exception {
        //创建相关流
        FileInputStream fileInputStream = new FileInputStream("a.png");
        FileOutputStream fileOutputStream = new FileOutputStream("b.png");

        //获取各个流对应的fileChannel
        FileChannel sourceCh = fileInputStream.getChannel();
        FileChannel destCh = fileOutputStream.getChannel();

        //使用transferFrom完成拷贝
        destCh.transferFrom(sourceCh, 0, sourceCh.size());

        //关闭相关通道和流
        sourceCh.close();
        destCh.close();
        fileInputStream.close();
        fileOutputStream.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值