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 function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Python实现文件下载、Cookie以及重定向的方法代码

《Python实现文件下载、Cookie以及重定向的方法代码》本文主要介绍了如何使用Python的requests模块进行网络请求操作,涵盖了从文件下载、Cookie处理到重定向与历史请求等多个方面,... 目录前言一、下载网络文件(一)基本步骤(二)分段下载大文件(三)常见问题二、requests模块处理

Linux内存泄露的原因排查和解决方案(内存管理方法)

《Linux内存泄露的原因排查和解决方案(内存管理方法)》文章主要介绍了运维团队在Linux处理LB服务内存暴涨、内存报警问题的过程,从发现问题、排查原因到制定解决方案,并从中学习了Linux内存管理... 目录一、问题二、排查过程三、解决方案四、内存管理方法1)linux内存寻址2)Linux分页机制3)

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

使用DeepSeek API 结合VSCode提升开发效率

《使用DeepSeekAPI结合VSCode提升开发效率》:本文主要介绍DeepSeekAPI与VisualStudioCode(VSCode)结合使用,以提升软件开发效率,具有一定的参考价值... 目录引言准备工作安装必要的 VSCode 扩展配置 DeepSeek API1. 创建 API 请求文件2.

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

Python判断for循环最后一次的6种方法

《Python判断for循环最后一次的6种方法》在Python中,通常我们不会直接判断for循环是否正在执行最后一次迭代,因为Python的for循环是基于可迭代对象的,它不知道也不关心迭代的内部状态... 目录1.使用enuhttp://www.chinasem.cnmerate()和len()来判断for