前言
在文章NIO 下的 ByteBuffer简单学习 中, 我们有写过一个简单的文件读取案例, 在案例中有使用到 Buffer 和 Channel, 关于 Buffer 的简单使用可以看下面两篇文章
- NIO 下的 ByteBuffer简单学习
- 最简单的 NIO 粘包解析与分析
关于 Channel 主要分为以下几种, 本篇文章是对 FileChannel 的讲解:
- FileChannel: 文件通道, 主要用于对文件的读写
- DatagramChannel: 数据包通道, 可以发送和接受 UPD 的数据包
- SocketChannel: 套接字通过, 接收 TCP 数据包的读写
- ServerSocketChannel: 服务器套接字通道, 监听新进来的 TCP 连接, 为每一个新连接都创建一个 SocketChannel
FileChannel
注意: FileChannel 只能工作在阻塞模式下
新建
FileChannel 是一个抽象类, 所以不能直接创建对象
1
2
3
4
5
6
7
8
|
public abstract class FileChannel extends AbstractInterruptibleChannel implements SeekableByteChannel, GatheringByteChannel,ScatteringByteChannel { /** * Initializes a new instance of this class. * */ protected FileChannel(){} |
创建一个 FileChannel 有以下三种方式:
- 创建一个 FileInputStream 对象, 但是该对象获取到的 Channel 只能读取
- 创建一个 FileOutputStream 对象, 但是该对象获取到的 Channel 只能写入
- 创建一个 RandomAccessFile 对象, 该对象能否读写是根据构造 RandomAccessFile 时设置的读写模式设定的
注意: Channel 使用之后必须关闭
不主动 close 并且 未触发gc , 那么 连接 和 句柄 将被 一直占用, 如果此时使用的是连接池方式, 将造成连接池中的连接不能及时的被回收问题
在调用 FileInputStream 、 FileOutputStream 和 RandomAccessFile 的 close 方法会间接调用 Channel 的 close 方法
实现文件的读写
通过以下代码可以简单的通过 FileInputStream 和 FileOutputStream 来实现对文件的读写
1
2
3
4
5
6
7
8
9
10
11
|
public static void main(String[] args) { try ( FileChannel inputChannel = new FileInputStream( name: "test1.txt" ).getChannel(); FileChannel outputChannel = new FileOutputStream( name: "test2.txt" ).getChannel() ){ // inputChannel 流读取到的内容通过 outputChannel 传输到指定的地址 inputChannel.transferTo( position: 0 ,inputChannel.size(),outputChannel); } catch (Exception e){ e.printstackTrace(); } } |
但是这里有个问题, FileChannel 的 transferTo 只能传输 2G 以内的数据, 超过 2G 就传输不了了,
下面是 FileChannel 的 tarnsferTo 方法, 可以看到他是存在返回值的, 这个返回值就代表着还剩下多少字节的内容没有进行传输, 所以我们可以使用一个 for 循环来对当前的代码进行改进
改进后的代码如下所示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public static void main(String[] args) { try ( FileChannel inputChannel = new FileInputStream( name: "test1.txt" ).getChannel(); FileChannel outputChannel = new FileOutputStream( name: "test2.txt" ).getChannel() ){ //获取到 输入流 的大小 long size = inputChannel.size(); // res 代表剩余多少字节没有进行传输 for ( long res = size;res > 0 ; ){ //将 inputChannel 流读取到的内容通过 outputChannel 传输到指定的地址 // 效率高,底层会利用作系统的 零拷贝 进行优化,但是一次只能传输 2G 的数据 // 该方法返回值为 剩余未传输的 字节数 res = inputChannel.transferTo( position: 0 ,inputChannel.size(), outputChannel); } } catch (Exception e){ e.printstackTrace(); } } |
本篇文章所有代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public static void main(String[] args) { try ( FileChannel inputChannel = new FileInputStream( "test1.txt" ).getChannel(); FileChannel outputChannel = new FileOutputStream( "test2.txt" ).getChannel() ){ // 获取到 输入流 的大小 long size = inputChannel.size(); // res 代表剩余多少字节没有进行传输 for ( long res = size; res > 0 ; ){ // 将 inputChannel 流读取到的内容通过 outputChannel 传输到指定的地址 // 效率高, 底层会利用操作系统的 零拷贝 进行优化, 但是一次只能传输 2G 的数据 // 该方法返回值为 剩余未传输的 字节数 res = inputChannel.transferTo( 0 , inputChannel.size(), outputChannel); } } catch (Exception e){ e.printStackTrace(); } } |
到此这篇关于详解NIO中FileChannel文件流的简单使用的文章就介绍到这了,更多相关FileChannel使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://juejin.cn/post/7164545691488878623