FileChannel主要方法的使用

2024-01-13 15:18

本文主要是介绍FileChannel主要方法的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

FileChannel介绍

FileChannel类是Channel接口的主要实现类,Channel是NIO的重要组件之一。
FileChannel类的主要作用是读取、写入、映射、操作文件。

write

public abstract int write(ByteBuffer src) throws IOException;

FileChannel内部维护了一个当前文件的position,可以查询、修改position。

/*** int write(ByteBuffer src)将src的remaining的字节写入FileChannel的position。* @throws Exception*/
@Test
public void testWrite() throws Exception {FileOutputStream fos = new FileOutputStream("d:/1.txt");FileChannel fileChannel = fos.getChannel();ByteBuffer b = ByteBuffer.wrap(new byte[]{1,2,3,4,5});int result = fileChannel.write(b);System.out.println("写入了" + result+ "个字节");  // 1,2,3,4,5System.out.println("fileChannel.position()=" + fileChannel.position());fileChannel.position(2);//设置FileChannel的position位置是2b.rewind();result = fileChannel.write(b); //1,2,1,2,3,4,5System.out.println("写入了" + result+ "个字节");b.position(4);result = fileChannel.write(b);System.out.println("写入了" + result+ "个字节"); //1,2,1,2,3,4,5,5fos.close();
}
/*** int write(ByteBuffer src)是同步的,同一时间只能有一个线程写入* @throws Exception*/
@Test
public void testWrite2() throws Exception {FileOutputStream fos = new FileOutputStream("d:/1.txt");FileChannel fileChannel = fos.getChannel();ExecutorService exec = Executors.newFixedThreadPool(100);for (int i = 0; i < 200; i++) {exec.execute(()->{int i2 = new Random().nextInt(2);ByteBuffer byteBuffer;if(i2 % 2 == 0){byteBuffer = ByteBuffer.wrap((Thread.currentThread().getName() + "写入abcde\n").getBytes());}else{byteBuffer = ByteBuffer.wrap((Thread.currentThread().getName() + "写入12345\n").getBytes());}try {Thread.sleep(1);fileChannel.write(byteBuffer);} catch (Exception e) {e.printStackTrace();}});}exec.shutdown();while(!exec.isTerminated()){Thread.sleep(1000);}
}

在这里插入图片描述
写入的文件没有发生数字和字母混合在同一行的情况。

其他write方法

write(ByteBuffer[] srcs) 将ByteBuffer数组srcs里的内容(都是从position到limit处的内容)批量写到当前FileChannel的position开始处。
write(ByteBuffer src, long position)向FileChannel通道的指定position写入src的remaining内容。

read()

和write()一样,同一时间只能有1个线程读取,其他线程阻塞。

public abstract int read(ByteBuffer dst) throws IOException

read方法返回读取了多少个字节,如果是末尾,返回-1。如果dst的remaining用完了,返回0.
read方法会将FileChannel当前position读入1个到dst的remaining字节空间。

/*** 假设1.txt里写入了5个字节:* ByteBuffer b = ByteBuffer.wrap(new byte[]{1,2,3,4,5});* fileChannel.write(b);* @throws Exception*/
@Test
public void testRead() throws Exception {FileInputStream fis = new FileInputStream("d:/1.txt");FileChannel fileChannel = fis.getChannel();ByteBuffer byteBuffer = ByteBuffer.allocate(5);int resultBytesCount = fileChannel.read(byteBuffer);System.out.println("读取了" + resultBytesCount+"个字节");   //读取了5个字节System.out.println("fileChannel.position()=" + fileChannel.position()); //fileChannel.position()=5resultBytesCount = fileChannel.read(byteBuffer);System.out.println("读取了" + resultBytesCount+"个字节");  //读取了0个字节System.out.println("fileChannel.position()=" + fileChannel.position()); //fileChannel.position()=5byteBuffer.clear();resultBytesCount = fileChannel.read(byteBuffer);System.out.println("读取了" + resultBytesCount+"个字节");  //读取了-1个字节,因为FileChannel已经到头了System.out.println("fileChannel.position()=" + fileChannel.position()); //fileChannel.position()=5fileChannel.position(1);byteBuffer.position(1);resultBytesCount = fileChannel.read(byteBuffer); //从byteBuffer的position=1处开始读,读入到fileChannel的position=1处System.out.println("读取了" + resultBytesCount+"个字节");  //读取了4个字节System.out.println("fileChannel.position()=" + fileChannel.position()); //fileChannel.position()=5fileChannel.close();;fis.close();
}

在这里插入图片描述

其他read方法

read(ByteBuffer[] dsts) 将FileChannel的当前position开始字节读取到,ByteBuffer数组dsts的每一个ByteBuffer的position到limit。
int read(ByteBuffer dst, long position) 将FileChannle的指定position开始的内容读入到dst的position处。

size

long size() 返回此FileChannel关联文件的大小

例如使用FileChannel完成文件的读取

/*** 假定文件已经存在,使用ByteBuffer和FileChannel将文件中的数据读入到程序,并显示在控制台屏幕。*/
@Test
public void testSize() throws Exception {File file = new File("d:\\1.txt");FileInputStream fileInputStream = new FileInputStream(file);FileChannel fileChannel = fileInputStream.getChannel();ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());fileChannel.read(byteBuffer);//将byteBuffer 的 字节数据 转成StringSystem.out.println(new String(byteBuffer.array()));fileInputStream.close();
}/*** 使用 FileChannel完成文件的拷贝。拷贝一个文件 1.txt*/
public void testReadAndWrite() throws Exception{FileInputStream fileInputStream = new FileInputStream("1.txt");FileChannel fileChannel1 = fileInputStream.getChannel();FileOutputStream fileOutputStream = new FileOutputStream("2.txt");FileChannel fileChannel2 = fileOutputStream.getChannel();ByteBuffer byteBuffer = ByteBuffer.allocate(512);while (true) {byteBuffer.clear(); //清空bufferint read = fileChannel1.read(byteBuffer);if(read == -1) { //表示读完break;}byteBuffer.flip();fileChannel2.write(byteBuffer);}fileInputStream.close();fileOutputStream.close();
}

transFrom & transTo

long transferFrom(ReadableByteChannel src,long position, long count) 将数据从src通道中的position开始传输最大count个字节到当前FileChannel通道。
long transferTo(long position, long count,WritableByteChannel target) 将FileChannel中position处开始的字节传输最大count个字节到另外一个通道。如果position>FileChannel大小,不传输任何字节。

用transFrom完成文件的拷贝

/*** 使用 FileChannel(通道) 和 方法  transferFrom ,完成文件的拷贝*/
public void testTransferFrom() throws Exception{FileInputStream fileInputStream = new FileInputStream("d:\\1.txt");FileOutputStream fileOutputStream = new FileOutputStream("d:\\1.txt");FileChannel sourceCh = fileInputStream.getChannel();FileChannel destCh = fileOutputStream.getChannel();//使用transferForm完成拷贝destCh.transferFrom(sourceCh,0,sourceCh.size());//关闭相关通道和流sourceCh.close();destCh.close();fileInputStream.close();fileOutputStream.close();
}

这篇关于FileChannel主要方法的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/601898

相关文章

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MyBatis ParameterHandler的具体使用

《MyBatisParameterHandler的具体使用》本文主要介绍了MyBatisParameterHandler的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参... 目录一、概述二、源码1 关键属性2.setParameters3.TypeHandler1.TypeHa

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

使用Python实现Word文档的自动化对比方案

《使用Python实现Word文档的自动化对比方案》我们经常需要比较两个Word文档的版本差异,无论是合同修订、论文修改还是代码文档更新,人工比对不仅效率低下,还容易遗漏关键改动,下面通过一个实际案例... 目录引言一、使用python-docx库解析文档结构二、使用difflib进行差异比对三、高级对比方

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

504 Gateway Timeout网关超时的根源及完美解决方法

《504GatewayTimeout网关超时的根源及完美解决方法》在日常开发和运维过程中,504GatewayTimeout错误是常见的网络问题之一,尤其是在使用反向代理(如Nginx)或... 目录引言为什么会出现 504 错误?1. 探索 504 Gateway Timeout 错误的根源 1.1 后端