一 , FileChanle
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的更多相关文章
- Java NIO 文件通道 FileChannel 用法
FileChannel 提供了一种通过通道来访问文件的方式,它可以通过带参数 position(int) 方法定位到文件的任意位置开始进行操作,还能够将文件映射到直接内存,提高大文件的访问效率.本文将 ...
随机推荐
- 华企盾DSC导致Solidworks无法导出xls文件
将Solidworks进程和Excel进程的OLE控制和启用虚拟重定向都关闭
- Bert-vits2最终版Bert-vits2-2.3云端训练和推理(Colab免费GPU算力平台)
对于深度学习初学者来说,JupyterNoteBook的脚本运行形式显然更加友好,依托Python语言的跨平台特性,JupyterNoteBook既可以在本地线下环境运行,也可以在线上服务器上运行.G ...
- 丝丝入扣,毫不违和,AI一键换脸和微调,基于Rope-Ruby,2024最新整合包
AI换脸已经不是什么时新的技术了,从DeepFace到Facesweap,再到Roop.AI换脸技术中出现了一种名为"一键换脸"的方法,它不需要训练模型.这种方法利用了名为&quo ...
- linux中iptables防火墙相关命令
https://www.cnblogs.com/seven1979/p/4173927.html https://blog.csdn.net/shenjianxz/article/details/62 ...
- 文心一言 VS 讯飞星火 VS chatgpt (174)-- 算法导论13.3 3题
三.用go语言,假设图13-5 和图13-6 中子α.β.γ.δ和ε的黑高都是k.给每张图中的每个结点标上黑高,以验证图中所示的转换能保持性质 5. 文心一言: 在Go语言中,你可以通过以下步骤来为图 ...
- 一文了解Vprix容器流媒体平台和传统云桌面的区别、优劣势
在当今数字化时代,随着云计算和远程办公的兴起,云桌面项目成为了提升工作效率和灵活性的重要工具.云桌面项目通过将用户的桌面环境和应用程序虚拟化,为用户提供了随时随地访问个人工作环境的便利.本文将介绍Vp ...
- 聊聊ChatGLM中P-tuning v2的应用
论文PDF地址:https://arxiv.org/pdf/2110.07602.pdf 转载请备注出处:https://www.cnblogs.com/zhiyong-ITNote/ P-Tunin ...
- 直接在*.vue文件(SFC)中使用JSX/TSX渲染函数,真香!
前言 在日常开发中vue的模版语法在大多数情况都能够满足我们的需求,但是在一些复杂的业务场景中使用模版语法就有些麻烦了.这个时候灵活的JSX/TSX渲染函数就能派上用场了,大多数同学的做法都是将*.v ...
- 详解GaussDB(DWS)通信安全的小妙招:连接认证机制
本文分享自华为云社区<GaussDB(DWS)数据库安全系列之通信安全>,作者:yd_262982826. 1. 前言 适用版本:[8.1.3及以上] 网络是一个开放的环境,仅仅依靠用户名 ...
- 华为云弹性云服务器ECS搭建FTP服务实践
摘要:在使用华为弹性云服务器ECS搭建FTP服务的时候,经常会遇到搭建完成后无法访问的问题.本篇通过演示windows IIS搭建FTP方法,讲解ftp主动模式.被动模式原理来说明无法访问的原因及解决 ...