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. 【C#】【System.IO】关于拷贝文件夹以及(Directory和DirectoryInfo、File和FileInfo)的区别

    本次问题是想要拷贝文件夹,但是找了一圈发现只有File有Copy或者FileInfo的CopyTo,并没有Directory的拷贝操作方法. 针对C#中拷贝文件夹的方法就是先生成一个目标文件夹(des ...

  2. 神经网络优化篇:详解其他正则化方法(Other regularization methods)

    其他正则化方法 除了\(L2\)正则化和随机失活(dropout)正则化,还有几种方法可以减少神经网络中的过拟合: 一.数据扩增 假设正在拟合猫咪图片分类器,如果想通过扩增训练数据来解决过拟合,但扩增 ...

  3. 聊聊流式数据湖Paimon(四)

    Partial Update 数据打宽 通过不同的流写不同的字段,打宽了数据的维度,填充了数据内容:如下所示: --FlinkSQL参数设置 set `table.dynamic-table-opti ...

  4. 1.7每日总结-vue链mysql4

    新建/server/router.js,用于配置对应路由let express = require('express')let router = express.Router()let user = ...

  5. Baby_Step_Gaint_Step(BSGS) 算法

    \(BSGS\) 算法,又称 "北(\(B\))上(\(S\))广(\(G\))深(\(S\))" 算法,"拔山盖世"算法,可以在 \(O(\sqrt{n})\ ...

  6. [西湖论剑2023-Misc] 复现

    MISC mp3 题目 我的解答: 010发现mp3藏有png图片 卡里分离得到图片 foremost cipher.mp3 zsteg发现里面有压缩包 提取出来 zsteg -e b1,r,lsb, ...

  7. MySQL篇:第六章_详解mysql视图

    周末有朋友来上海没来得及更新,特此更两篇以正身 视图 含义:理解成一张虚拟的表 视图和表的区别: 使用方式 占用物理空间 视图 完全相同 不占用,仅仅保存的是sql逻辑 表 完全相同 占用 视图的好处 ...

  8. 掌数科技携手华为云GaussDB,助力金融科技创新,联合打造行业标杆

    本文分享自华为云社区<掌数科技携手华为云GaussDB,助力金融科技创新,联合打造行业标杆>,作者:GaussDB 数据库 . 近日,在华为开发者大会2023(Cloud)的"G ...

  9. Python从零到壹丨带你了解图像直方图理论知识和绘制实现

    摘要:本文将从OpenCV和Matplotlib两个方面介绍如何绘制直方图,这将为图像处理像素对比提供有效支撑. 本文分享自华为云社区<[Python从零到壹] 五十.图像增强及运算篇之图像直方 ...

  10. 小熊派开发实践丨漫谈LiteOS之传感器移植

    摘要:本文基于小熊派开发板简单介绍了如何在LiteOS中移植传感器,从而实现对于传感器的相关控制. 1 hello world 相信大家无论在学习编程语言开始的第一个函数应该是HelloWorld,本 ...