当前位置: 首页 > news >正文

【Java开发日记】我们来讲一讲 Channel 和 FileChannel

目录

Channel

FileChannel

打开 FileChannel

从 FileChannel 读数据

写数据到 FileChannel

关闭 FileChannel

示例

读数据

写数据


Channel

在 NIO 中,Channel 和 Buffer 是相辅相成的,只能从 Channel 读取数据到 Buffer 中,或者从 Buffer 写入数据到 Channle,如下图:


Channel 类似于 OIO 中的流(Stream),但是又有所区别:

  • 流是单向的,但 Channel 是双向的,可读可写。
  • 流是阻塞的,但 Channle 可以异步读写。
  • 流中的数据可以选择性的先读到缓存中,而 Channel 的数据总是要先读到一个 Buffer 中,或从 Buffer 中写入,如上图。

NIO 中通过 Channel 封装了对数据源的操作,通过 Channel 可以操作数据源,但是又不必关注数据源的具体物理结构,这个数据源可以是文件,也可以是socket。
Channel 的接口定义如下:

publicinterface Channel extends Closeable {public boolean isOpen();public void close() throws IOException;
}

Channel 接口仅定义两个方法:

  • isOpen():Channel 是否打开
  • close():关闭 Channel

它的主要实现有:

  • FileChannel:文件通道,用于文件的数据读写。
  • SocketChannel:套接字通道,能通过 TCP 读写网络中的数据。
  • ServerSocketChannel:服务器套接字通道,监听新进来的 TCP 连接,像 web 服务器那样,对每一个新进来的连接都会创建一个 SocketChannel
  • DatagramChannel:数据报通道,能通过 UDP 读写网络中的数据。

基本类图如下:


下面就 FileChannel 做详细介绍。 

FileChannel

FileChannel 主要是用来读写和映射一个系统文件的 Channel,它是一个抽象类,具体由 FileChannelImpl 来实现。
定义如下:

package java.nio.channels;
publicabstractclass FileChannelextends AbstractInterruptibleChannelimplements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel{/*** 初始化一个无参构造器.*/protected FileChannel() { }//打开或创建一个文件,返回一个文件通道来访问文件public static FileChannel open(Path path,Set<? extends OpenOption> options,FileAttribute<?>... attrs)throws IOException{FileSystemProvider provider = path.getFileSystem().provider();return provider.newFileChannel(path, options, attrs);}privatestaticfinal FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];//打开或创建一个文件,返回一个文件通道来访问文件public static FileChannel open(Path path, OpenOption... options)throws IOException{Set<OpenOption> set = new HashSet<OpenOption>(options.length);Collections.addAll(set, options);return open(path, set, NO_ATTRIBUTES);}//从这个通道读入一个字节序列到给定的缓冲区public abstract int read(ByteBuffer dst) throws IOException;//从这个通道读入指定开始位置和长度的字节序列到给定的缓冲区public abstract long read(ByteBuffer[] dsts, int offset, int length)throws IOException;/*** 从这个通道读入一个字节序列到给定的缓冲区*/public final long read(ByteBuffer[] dsts) throws IOException {return read(dsts, 0, dsts.length);}/*** 从给定的缓冲区写入字节序列到这个通道*/public abstract int write(ByteBuffer src) throws IOException;/*** 从给定缓冲区的子序列向该信道写入字节序列*/public abstract long write(ByteBuffer[] srcs, int offset, int length)throws IOException;/*** 从给定的缓冲区写入字节序列到这个通道*/public final long write(ByteBuffer[] srcs) throws IOException {return write(srcs, 0, srcs.length);}/*** 返回通道读写缓冲区中的开始位置*/public abstract long position() throws IOException;/*** 设置通道读写缓冲区中的开始位置*/public abstract FileChannel position(long newPosition) throws IOException;/*** 返回此通道文件的当前大小*/public abstract long size() throws IOException;/*** 通过指定的参数size来截取通道的大小*/public abstract FileChannel truncate(long size) throws IOException;/*** 强制将通道中的更新文件写入到存储设备(磁盘等)中*/public abstract void force(boolean metaData) throws IOException;/*** 将当前通道中的文件写入到可写字节通道中* position就是开始写的位置,long就是写的长度*/public abstract long transferTo(long position, long count,WritableByteChannel target)throws IOException;/*** 将当前通道中的文件写入可读字节通道中* position就是开始写的位置,long就是写的长度*/public abstract long transferFrom(ReadableByteChannel src,long position, long count)throws IOException;/*** 从通道中读取一系列字节到给定的缓冲区中* 从指定的读取开始位置position处读取*/public abstract int read(ByteBuffer dst, long position) throws IOException;/*** 从给定的缓冲区写入字节序列到这个通道* 从指定的读取开始位置position处开始写*/public abstract int write(ByteBuffer src, long position) throws IOException;// -- Memory-mapped buffers --/*** 一个文件映射模式类型安全枚举*/publicstaticclass MapMode {//只读映射模型publicstaticfinal MapMode READ_ONLY= new MapMode("READ_ONLY");//读写映射模型publicstaticfinal MapMode READ_WRITE= new MapMode("READ_WRITE");/*** 私有模式(复制在写)映射*/publicstaticfinal MapMode PRIVATE= new MapMode("PRIVATE");privatefinal String name;private MapMode(String name) {this.name = name;}}/*** 将该通道文件的一个区域直接映射到内存中*/public abstract MappedByteBuffer map(MapMode mode,long position, long size)throws IOException;/*** 获取当前通道文件的给定区域上的锁* 区域就是从position处开始,size长度 * shared为true代表获取共享锁,false代表获取独占锁*/public abstract FileLock lock(long position, long size, boolean shared)throws IOException;/*** 获取当前通道文件上的独占锁*/public final FileLock lock() throws IOException {return lock(0L, Long.MAX_VALUE, false);}/*** 尝试获取给定的通道文件区域上的锁* 区域就是从position处开始,size长度 * shared为true代表获取共享锁,false代表获取独占锁*/public abstract FileLock tryLock(long position, long size, boolean shared)throws IOException;/*** 尝试获取当前通道文件上的独占锁*/public final FileLock tryLock() throws IOException {return tryLock(0L, Long.MAX_VALUE, false);}
}

打开 FileChannel

在使用 FileChannle 之前必须要先打开它,但是无法直接打开一个 FileChannel,需要通过使用一个 InputStream、OutputStream、RandomAcessFile 来获取一个 FileChannel 实例,如下:

RandomAccessFile accessFile = new RandomAccessFile("/Users/chenssy/Documents/FileChannel.txt","rw");
FileChannel fileChannel = accessFile.getChannel();

调用 getChannel() 即可获取 FileChannel 实例,源码如下:

public final FileChannel getChannel() {synchronized (this) {if (channel == null) {channel = FileChannelImpl.open(fd, path, true, rw, this);}return channel;}
}

getChnnel() 方法很简单,直接调用 FileChannelImpl 的静态方法 open()

public static FileChannel open(Path path,Set<? extends OpenOption> options,FileAttribute<?>... attrs) throws IOException{FileSystemProvider provider = path.getFileSystem().provider();return provider.newFileChannel(path, options, attrs);
}

从 FileChannel 读数据

调用 FileChannel 的 read() 方法即可从 FileChannel 中获取数据,当然不是直接获取,而是需要先写入到 Buffer 中,所以调用 read() 之前,需要分配一个 Buffer,然后调用 read() ,该方法返回 int 表示有多少数据读取到了 Buffer 中了,如果返回 -1 表示已经到文件末尾了。

ByteBuffer buffer = ByteBuffer.allocate(1024);
int readCount = fileChannel.read(buffer);

FileChannel 仅定义了方法,具体实现在 FileChannelImpl,如下:

public int read(ByteBuffer dst) throws IOException {ensureOpen();if (!readable)thrownew NonReadableChannelException();// 加锁synchronized (positionLock) {int n = 0;int ti = -1;try {begin();ti = threads.add();if (!isOpen())return0;do {// 通过IOUtil.read实现n = IOUtil.read(fd, dst, -1, nd);} while ((n == IOStatus.INTERRUPTED) && isOpen());return IOStatus.normalize(n);} finally {threads.remove(ti);end(n > 0);assert IOStatus.check(n);}}
}

如果小假的内容对你有帮助,请点赞评论收藏。创作不易,大家的支持就是我坚持下去的动力!

http://www.dtcms.com/a/348716.html

相关文章:

  • Polkadot - JAM
  • 美股期权历史市场数据波动特性分析
  • 【中文教材】14. 汇率计算
  • 人工智能-python-深度学习-tensor基操
  • 数学建模(摸索中……)
  • CUDA安装,pytorch库安装
  • 如何实现模版引擎
  • Shell 学习笔记 - Shell 三剑客篇
  • unity热更新总结
  • 【如何使用Redis实现分布式锁详解讲解】
  • [快乐数](哈希表)
  • 解决编译osgEarth中winsocket2.h找不到头文件问题
  • 基于Spark的热门旅游景点数据分析系统的设计-django+spider
  • Spring Boot测试陷阱:失败测试为何“传染”其他用例?
  • 【追涨抄底关注】副图指标 紫色主力线上行表明资金介入明显 配合价格突破时可靠性更高
  • deepseek连接solidworks设计一台非标设备 (part1)
  • 阿里云ECS服务器搭建ThinkPHP环境
  • 互联网大厂AI/大模型应用开发工程师面试剧本与解析
  • 阿里云云数据库RDS PostgreSQL管控功能使用
  • 基于SpringBoot的婚纱影楼服务预约平台【2026最新】
  • Spring AI 学习笔记(2)
  • GitHub 热榜项目 - 日榜(2025-08-24)
  • Wireshark USRP联合波形捕获(下)
  • windows上如何实现把指定网段的流量转发到指定的端口,有哪些界面化的软件用来配置完成,类似于 Linux中的iptables规则实现
  • 6.1Element UI布局容器
  • 【Luogu】P2602 [ZJOI2010] 数字计数 (数位DP)
  • 基于大模型的对话式推荐系统技术架构设计-- 大数据平台层
  • 07 - spring security基于数据库的账号密码
  • window11无法连接Fortinet SSL VPN
  • Elasticsearch如何确保数据一致性?