一 , 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) 方法定位到文件的任意位置开始进行操作,还能够将文件映射到直接内存,提高大文件的访问效率.本文将 ...
随机推荐
- 某RBAC管理系统审计
某RBAC管理系统审计 前言 这个管理系统的审计我去年就开始了但烂尾了,那时候太热闹了log4j2,cs的cve反制等等.这个都给忘了,所以本篇可能有些图有点老,现在就是旧图没一个个换遇到的新的就加上 ...
- 1.elasticsearch运行
在docker中运行elasticsearch.kibana 一.MacOs 首先需要安装doceker,提供两种方式,选一种方便的就好 1.命令行安装方式 安装命令行 xcode-select -- ...
- CSS 基础 2 - Box Model 盒模型
CSS 基础 2 - Box Model 盒模型 Box Model 盒模型是网页布局的前提.网页布局的几种方式: Table(过时):在 CSS 出现之前,一般用表格布局(将表格边框线隐藏) Flo ...
- flutter中InheritedWidget共享数据
InheritedWidget是Flutter框架中用于在Widget树中共享数据的机制.它是一个特殊的Widget,可以将其放置在Widget树的上层,并向下传递共享的数据给其子Widget.子Wi ...
- root无法下载
在 Linux 中,root 用户已经拥有系统中的最高权限.然而,如果你需要确保 root 用户具有所有权限,可以通过以下步骤来进行:(这是不对的) sudo chown -R root:root / ...
- vue 遍历的汉字显示不同的颜色
<template> <div> <div class="stars"> <span v-for="(star, index) ...
- JavaScript异步编程4——Promise错误处理
目录 1. 概述 2. 详论 3. 参考 1. 概述 在上一篇文章<JavaScript异步编程3--Promise的链式使用>中,通过Promise的链式使用,避免程序中多次嵌套回调(回 ...
- 记录:websoket切换页面后重复执行问题
问题描述 因为项目需求,实时播放执行信息.而项目的websoket只在这个页面,会有切换情况.从websoket连接得到执行列表数据.断开重连后会传递新连接数据+旧连接数据.也就是说,如果第一次进入页 ...
- 云图说|ModelArts开发环境,让AI开发、探索、教学更简单
摘要:ModelArts开发环境,以云原生的资源使用和开发工具链的集成,目标为不同类型AI开发.探索.教学用户. 本文分享自华为云社区<[云图说]| 第280期 ModelArts开发环境,让A ...
- 50亿海量数据如何高效存储和分析? GaussDB (for Cassandra) 3个秘诀搞定
摘要:信息社会正在从互联网时代走向物联网时代,企业不可避免的要面对数据量剧增带来的一系列问题:如何高效存储和扩容,如何在对原有业务改动最小的情况下做到智能化和实时分析. 本文分享自华为云社区<5 ...