【Netty】NIO 通道 ( Channel ) 组件(二)

简介: 【Netty】NIO 通道 ( Channel ) 组件(二)

VI . 文件通道 ( FileChannel ) 读取文件 示例代码


image.png


1 . 示例需求 : 通过 文件通道 ( FileChannel ) 读取文件中的数据 ;



① 文件通道 ( FileChannel ) 获取 : NIO 中 , 文件通道 ( FileChannel ) 可以从 文件输入流 ( FileInputStream ) 中进行获取 , 其本质是通过文件输入流 , 读取文件中的数据 ;


② 整体流程 : 先通过文件输入流获取文件通道 ( FileChannel ) , 文件通道 ( FileChannel ) 读取文件数据到 字节缓冲区 ( ByteBuffer ) 中 , 从 字节缓冲区 ( ByteBuffer ) 中获取数据 , 将该数据转为字符串打印出来 ;



2 . 代码示例 :


package kim.hsl.nio;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileChannelDemo2 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            //1 . FileChannel 可以从 FileInputStream 中获取
            fis = new FileInputStream("file.txt");
            //2 . 创建 FileChannel , 从 FileInputStream 中可以获取到
            //FileChannel 是抽象类 , 实际类型是 FileChannelImpl
            FileChannel fc = fis.getChannel();
            //3 . FileChannel 需要通过 缓冲区 Buffer 才能与数据进行读写交互
            ByteBuffer buffer = ByteBuffer.allocate(32);
            //4 . 将 字节缓冲区 ByteBuffer 中的数据写入到 文件通道 FileChannel 中
            int len = fc.read(buffer);
            //5 . 将数据从缓冲区中取出
            byte[] stringData = new byte[len];
            //注意 : ByteBuffer 需要 flip 翻转后才能读取
            buffer.flip();
            buffer.get(stringData);
            //6 . byte 数组数据转为字符串并打印出来
            String fileString = new String(stringData);
            System.out.println(fileString);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


执行结果 :


Hello World






VII . 文件通道 ( FileChannel ) 使用 缓冲区 拷贝文件 示例代码


image.png


1 . 示例需求 : 通过 文件通道 ( FileChannel ) 与 字节缓冲区 ( ByteBuffer ) 进行文件拷贝 ;



① 文件通道 ( FileChannel ) 获取 : NIO 中 , 文件通道 ( FileChannel ) 可以从 文件输入流 ( FileInputStream ) 中进行获取 , 也可以从 文件输出流 ( FileOutputStream ) 中获取 , 其本质是通过文件输入流 , 读取文件中的数据 ;


② 整体流程 :


先通过文件输入流获取 输入文件通道 ( FileChannel ) , 通过文件输出流获取 输出文件通道 ( FileChannel ) ;

文件通道 ( FileChannel ) 读取文件数据到 字节缓冲区 ( ByteBuffer ) 中

输入文件通道读取数文件据到缓冲区中 , 输出文件通道写出缓冲区数据到文件中 ;


2 . 代码示例 :


package kim.hsl.nio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileChannelDemo3 {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //1 . FileChannel 可以从 FileInputStream 中获取
            fis = new FileInputStream("file.txt");
            fos = new FileOutputStream("file2.txt");
            //2 . 创建 FileChannel , 从 FileInputStream / FileOutputStream 中可以获取到
            //FileChannel 是抽象类 , 实际类型是 FileChannelImpl
            FileChannel fcIn = fis.getChannel();
            FileChannel fcOut = fos.getChannel();
            //3 . FileChannel 需要通过 缓冲区 Buffer 才能 读写文件
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            //4 . 读取 file.txt 文件数据到 字节缓冲区 ByteBuffer , 并写出到 file2.txt 文件中
            //循环退出条件 : 如果 文件 读取完毕, read 方法会返回 -1, 代表读取文件完毕
            while ( (fcIn.read(buffer)) >= 0 ){
                //将 ByteBuffer 中的数据写出 file2.txt 文件中
                //翻转后, 将 position 设置成 0, 才能开始写出
                buffer.flip();
                fcOut.write(buffer);
                //重置标志位, 供下一次循环使用
                buffer.clear();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(fis != null)
                    fis.close();
                if(fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}



执行结果 :

image.png


目录
相关文章
|
2天前
|
存储 编解码 移动开发
技术笔记:NIO流—理解Buffer、Channel概念和NIO的读写操作
技术笔记:NIO流—理解Buffer、Channel概念和NIO的读写操作
10 1
|
2月前
|
设计模式 前端开发 网络协议
面试官:说说Netty的核心组件?
Netty 核心组件是指 Netty 在执行过程中所涉及到的重要概念,这些核心组件共同组成了 Netty 框架,使 Netty 框架能够正常的运行。 Netty 核心组件包含以下内容: 1. 启动器 Bootstrap/ServerBootstrap 2. 事件循环器 EventLoopGroup/EventLoop 3. 通道 Channel 4. 通道处理器 ChannelHandler 5. 通道管道 ChannelPipeline 这些组件的交互流程如下: ![image.png](https://cdn.nlark.com/yuque/0/2024/png/92791/1716
20 0
面试官:说说Netty的核心组件?
|
2月前
|
Java 应用服务中间件 API
从零手写实现 tomcat-06-servlet bio/thread/nio/netty 池化处理
该文介绍了逐步改进的网络服务器实现,从最初的 BIO 基础版到使用线程池的 BIO+Thread,再到 NIO 版本和 NIO+Thread,最后展示了一个使用 Netty 框架的简洁实现。文章旨在说明如何解决阻塞问题,并对比不同模型的优劣,最终推荐使用 Netty 以简化 NIO 编程。
|
2月前
|
编解码 网络协议 Java
用Java的BIO和NIO、Netty实现HTTP服务器(一) BIO与绪论
用Java的BIO和NIO、Netty实现HTTP服务器(一) BIO与绪论
|
2月前
|
移动开发 编解码 网络协议
用Java的BIO和NIO、Netty来实现HTTP服务器(三) 用Netty实现
用Java的BIO和NIO、Netty来实现HTTP服务器(三) 用Netty实现
|
2月前
|
前端开发 Java 网络安全
【Netty 网络通信】Netty 核心组件
【1月更文挑战第9天】【Netty 网络通信】Netty 核心组件
|
2月前
|
缓存 Java API
【Netty 网络通信】Channel 接口解析
【1月更文挑战第9天】【Netty 网络通信】Channel 接口解析
|
2月前
|
前端开发 网络协议 Java
Netty | 工作流程图分析 & 核心组件说明 & 代码案例实践
Netty | 工作流程图分析 & 核心组件说明 & 代码案例实践
143 0
|
2月前
|
设计模式 网络协议 Java
Java NIO 网络编程 | Netty前期知识(二)
Java NIO 网络编程 | Netty前期知识(二)
95 0
|
2月前
|
网络协议 前端开发 Java
Netty Review - 核心组件扫盲
Netty Review - 核心组件扫盲
82 0