package nio;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; /**
* @author DELL
*/
public class TestChannel {
public static void main(String[] args) throws IOException {
// read();
// write();
// transferFrom();
transferTo();
} /**
* 读取
* @throws IOException
*/
public static void read() throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\a.txt", "rw");
FileChannel channel = randomAccessFile.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(2);
int read = channel.read(byteBuffer);
while (read!=-1){
byteBuffer.flip();
while (byteBuffer.hasRemaining()){
System.out.print((char)byteBuffer.get());
}
byteBuffer.clear();
read = channel.read(byteBuffer);
} System.out.println();
channel.close();
randomAccessFile.close();
} /**
* 写入
* @throws IOException
*/
public static void write() throws IOException{ RandomAccessFile readFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\素书.txt", "rw");
RandomAccessFile writeFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\b.txt", "rw"); FileChannel readFileChannel = readFile.getChannel();
FileChannel writeFileChannel = writeFile.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1); int read = readFileChannel.read(buffer); while (read!=-1){
buffer.flip();
while (buffer.hasRemaining()){
writeFileChannel.write(buffer);
}
buffer.clear();
read = readFileChannel.read(buffer);
} readFileChannel.close();
writeFileChannel.close(); } /**
* 通道之间传输数据
*/
public static void transferTo () throws IOException{
RandomAccessFile readFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\素书.txt", "rw");
RandomAccessFile writeFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\b.txt", "rw"); FileChannel readFileChannel = readFile.getChannel();
FileChannel writeFileChannel = writeFile.getChannel(); readFileChannel.transferTo(0,readFileChannel.size(),writeFileChannel); readFileChannel.close();
writeFileChannel.close();
} /**
* 通道之间传输数据
*/
public static void transferFrom () throws IOException{
RandomAccessFile readFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\素书.txt", "rw");
RandomAccessFile writeFile = new RandomAccessFile("D:\\Maven-Item\\basic\\src\\b.txt", "rw"); FileChannel readFileChannel = readFile.getChannel();
FileChannel writeFileChannel = writeFile.getChannel(); writeFileChannel.transferFrom(readFileChannel,0,readFileChannel.size()); readFileChannel.close();
writeFileChannel.close();
} }

一 , FileChanle的更多相关文章

  1. Java NIO 文件通道 FileChannel 用法

    FileChannel 提供了一种通过通道来访问文件的方式,它可以通过带参数 position(int) 方法定位到文件的任意位置开始进行操作,还能够将文件映射到直接内存,提高大文件的访问效率.本文将 ...

随机推荐

  1. 强!推荐一款自动化神器Autolt:不再重复工作

    随着互联网不断发展,它给我们带来便利的同时,也带来了枯燥.重复.机械的重复工作.今天,我要和大家分享一款老牌实用的自动化工具:AutoIt,它能够让你告别繁琐的重复性工作,提高工作效率. 这里透露一下 ...

  2. 数字孪生和GIS融合会为各自带来什么样的改变?

    数字孪生和地理信息系统(GIS)是两个强大的技术,它们在各自领域发挥着重要作用.而当数字孪生与GIS融合时,将会为它们带来更加深远的改变和增益. 数字孪生技术以数字化的方式模拟和复制现实世界中的物理对 ...

  3. (数据科学学习手札156)地图可视化神器kepler.gl 3.0版本发布

    本文已收录至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,地图可视化神器kepler.gl终于带 ...

  4. 网安区过年-Log4j2

    Log4j2-2021 漏洞原理 Apache Log4j 2 是Java语言的日志处理套件,使用极为广泛.在其2.0到2.14.1版本中存在一处JNDI注入漏洞,攻击者在可以控制日志内容的情况下,通 ...

  5. Python——第二章:字符串操作——索引和切片

    索引: 按照位置提取元素 可以采用索引的方式来提取某一个字符(文字) s = "我叫周杰伦" print(s[3]) #程序员都是从0开始数,这里的3代表第4位,也就是" ...

  6. elasticsearch oom问题分析

    背景 线上发现elasticsearch集群状态red,并且有个es节点jvm内存使用不断升高,直到gc后依然内存不够使用,服务停止.查看日志,elasticsearch出现OOM报错. [2023- ...

  7. Spring Boot整合Spring Data连接postgreSQL完成简单的CRUD操作

    导入jpa依赖和postgresql依赖: <!-- jpa依赖 --> <dependency> <groupId>org.springframework.boo ...

  8. Spring 中循环依赖的处理

    Spring 提供了十分强大的依赖注入功能,使得我们不再需要手动去管理对象的依赖项.然而,在实际的使用场景中,可能会遇到类似下面的依赖异常: Exception encountered during ...

  9. 聊聊Llama2-Chinese中文大模型

    转载请注明出处:https://www.cnblogs.com/zhiyong-ITNote 基本简述 Llama2-Chinese 大模型:由清华.交大以及浙大博士团队领衔开发:基于200B中文语料 ...

  10. 2023-09-07:用go语言编写。塔子哥最近在处理一些字符串相关的任务 他喜欢 R 字符,因为在某些任务中,这个字符通常表示“正确”的结果 另一方面,他不喜欢 B 字符,因为在某些任务中,这个字符

    2023-09-07:用go语言编写.塔子哥最近在处理一些字符串相关的任务 他喜欢 R 字符,因为在某些任务中,这个字符通常表示"正确"的结果 另一方面,他不喜欢 B 字符,因为在 ...