一 , 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) 方法定位到文件的任意位置开始进行操作,还能够将文件映射到直接内存,提高大文件的访问效率.本文将 ...
随机推荐
- python tkinter 使用(七)
python tkinter 使用(七) 本篇文章主要讲下tkinter 中的message 控件. Message控件可以用于在窗口中显示一段文本消息. 以下是个简单的例子: #!/usr/bin/ ...
- 测试member和coupon的远程调用
测试member和coupon的远程调用 想要获取当前会员领取到的所有优惠券.先去注册中心找优惠券服务, 注册中心调一台优惠券服务器给会员,会员服务器发送请求给这台优 惠券服务器,然后对方响应. Fe ...
- Python——第二章:字符串操作——格式化
1. 字符串的格式化问题 举例:要按照如下格式输出一句话 我叫xxx, 我住在xxxx, 我今年xx岁, 我喜欢做xxxxx 这里首先引入占位符概念: %s 占位字符串%d 占位整数%f 占位小数 因 ...
- 3.CRUD及批量操作
文档的CRUD index 和 create 操作用put(但是如果没有指定id必须用post,指定了id的可以用post,也可以用put) update和创建索引用post 获取用get 删除用de ...
- 【K8S系列】如何高效查看 k8s日志
序言 你只管努力,其他交给时间,时间会证明一切. 文章标记颜色说明: 黄色:重要标题 红色:用来标记结论 绿色:用来标记一级论点 蓝色:用来标记二级论点 Kubernetes (k8s) 是一个容器编 ...
- git blame 用法小记
1.概述 git管理的代码仓库,在协作开发中不可避免地会出现代码冲突,或者有新手错误地提交代码.出现问题不可怕,可怕的是找不到问题出在哪里.有时候找到出问题的代码,却不知道是谁提交的.git提供了一个 ...
- 使用IO流写一个随机点名程序
前言 最近学习了关于IO流的一些知识点,但是应用还不够,所以今天做个简单案例: 随机创建名字导入文件中: package ioandcollection; import java.io.Buffere ...
- c#中用System.Diagnostics.Process.Start(Path.GetFullPath(“vlc.exe.lnk“), url);用vlc的快捷方式打开http的url不起作用?
vlc.exe.lnk双击这个文件,能正常打开vlc,但是用System.Diagnostics.Process.Start(Path.GetFullPath("vlc.exe.lnk&qu ...
- redis的基本数据类型测试
依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spri ...
- Java程序接入ChatGPT
目录 0 前言 1 还想体验的小伙伴可以试试 2 Java接入前准备 3 官方支持接入语言 4 调用费用 5 接口调用说明 6 代码实现 6.1 postman调用 6.2 Java调用 7 小结 0 ...