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. 将微服务注册到nacos中

    将微服务注册到nacos中将微服务注册到nacos中 首先修改pom文件,引入nacos依赖,名为spring-cloud-starter-nacos-discovery <dependency ...

  2. ElasticSearch之cat nodes API

    命令样例如下: curl -X GET "https://localhost:9200/_cat/nodes?v=true&pretty" --cacert $ES_HOM ...

  3. Recursion Function 递归笔记

    递归的解释: 递归(英语:Recursion),又译为递回, 在数学与计算机科学中,是指在函数的定义中使用函数自身的方法.(本文要讨论的重点) 递归一词还较常用于描述以自相似方法重复事物的过程.(指一 ...

  4. MySQL 基础(三)事务与 MVCC

    事务 事务是一组原子性的 SQL 操作,或者被称为一个独立的工作单元,如果数据库引擎能够成功地对数据库应用该组的全部 SQL 语句,那么就会全部执行,否则全部不执行. 事务的特性 在关系数据库管理系统 ...

  5. CodeForces 1307D BFS最短路 思维

    原题链接 题意 给出一个简单无向图,边权全部为1,同时给我们k个特殊点,要求我们从这k个特殊点中选出两个来连一条边权为1的边.同时,我们的决策要保证1~n的最短路程最大,求最终这个最短路长度. 思路 ...

  6. HDU 4893 线段树延迟标记

    原题链接 题意 初始有一长度为n,全为0的序列 每次我们可以对这个序列进行三种操作: 1.将某个位置的数加上d 2.输出某个区间的和 3.将某个区间内每个数字变为与其最接近的斐波那契数,如果有两个最相 ...

  7. 云原生2.0时代,华为云DevOps立体运维实践

    摘要:随着云原生2.0时代的来临,越来越多的企业及个人选择使用云原生技术来构建业务,云原生技术给业务构建.交付带了便利的同时,对运维也提出了更高的要求. 2020年12月,中国DevOps社区峰会在北 ...

  8. 带你掌握Visual Studio Code的格式化程序

    摘要:Visual Studio Code 中的所有语言都可以使用其中一种自动格式化程序进行格式化,并且 Python 扩展还支持 linter. 本文分享自华为云社区<Visual Studi ...

  9. 在线一键生成安卓证书keystore 文件

    在线一键生成安卓证书 keystore 文件 一般的打包工具都会有默认的安卓证书提供,但如果你需要上架需要用自己申请安卓证书 keystore 文件打包 apk 现有方便方便的工具,直接在网页就可以申 ...

  10. iOS 应用上架的步骤和工具简介

    编辑 APP开发助手是一款能够辅助iOS APP上架到App Store的工具,它解决了iOS APP上架流程繁琐且耗时的问题,帮助跨平台APP开发者顺利将应用上架到苹果应用商店.最重要的是,即使没有 ...