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. 数字孪生和GIS融合后能够为城市交通带来哪些便利?

    数字孪生和GIS的融合对于城市交通领域带来了诸多便利,从智能交通管理到出行体验的提升,为城市交通带来了全新的发展机遇. 首先,数字孪生技术与GIS的结合可以实现智能交通管理.通过GIS建立城市交通网络 ...

  2. Python——第四章:内置函数(下)

    内置函数的使用方法: locals:函数会以字典的类型返回当前位置的所有局部变量 globals:函数会以字典的类型返回全部局部变量 zip: 可以把多个可迭代内容进行合并 sorted: 排序 fi ...

  3. CSS3学习笔记引言

    开始我们要来介绍css: CSS(全称为Cascading Style Sheets)是一种用于描述HTML.XML等文档样式的样式语言,它能够定义元素的显示方式,如字体.颜色.布局等. CSS可以把 ...

  4. 基于FPGA的电子琴设计(按键和蜂鸣器)---第一版---郝旭帅电子设计团队

    本篇为各位朋友介绍基于FPGA的电子琴设计(按键和蜂鸣器)----第一版. 功能说明: 外部输入七个按键,分别对应音符的"1.2.3.4.5.6.7",唱作do.re.mi.fa. ...

  5. Asp .Net Core系列:AutoMapper自动映射框架介绍、使用

    1.介绍 AutoMapper是一个对象-对象映射器.对象-对象映射通过将一种类型的输入对象转换为另一种类型的输出对象来工作.使AutoMapper变得有趣的是,它提供了一些有趣的约定,以免去搞清楚如 ...

  6. Windows下编译并使用64位GMP

    目录 1. 概述 2. 编译 2.1. GCC环境 2.2. 编译过程 3. 使用 3.1. 调用 3.2. 示例 4. 参考 1. 概述 GMP是一个开源的数学运算库,它可以用于任意精度的数学运算, ...

  7. 如何通过一个SDK轻松搞定人脸识别,拯救初入职场的程序猿

    摘要:看一个SDK如何拯救初入职场的程序猿小Hi- [职场初体验] 时间过得真快,距离上次给小Hi安排"人脸识别"的开发任务(话接上期:[快速玩转华为云开发]小Hi拍了拍你,基于华 ...

  8. 华为云GaussDB:发挥生态优势,培养应用型DBA

    摘要:GaussDB首要的任务是解决华为的业务连续性的需求,同时也是要确保使用GaussDB的客户的业务能够连续,所以我们坚持战略投入,坚持从每一行代码,坚持从生态开始来构建整个数据库体系. 本文分享 ...

  9. 应用数仓ODBC前,这些问题你需要先了解一下

    摘要:ODBC为解决异构数据库间的数据共享而产生的,现已成为WOSA的主要部分和一种数据库访问接口标准. 本文分享自华为云社区<GaussDB(DWS) ODBC 问题定位指南>,作者: ...

  10. Taro架构构析(2):Taro 设计思想及架构

    从之前微信公众号的开发经验来说,以及腾讯开放平台的接入 等经验来看,一直对腾讯的开放文档 有心理阴影. 微信小程序从文件组织上看,一个小程序页面或组件,需要同时包含 4 个文件:脚本逻辑.样式.模板以 ...