NIO之FileChannel操作示例
1. 写文件操作
/**
* 写文件
*/
public class FileChannelTest {
public static void main(String[] args) throws IOException {
String str = "test file channel, 测试file channel";
// 创建一个输出流
FileOutputStream fileOutputStream = new FileOutputStream("D://nio.txt");
// 得到file channel
FileChannel fileChannel = fileOutputStream.getChannel();
// 创建一个缓存buffer ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// 将数据放到buffer中
byteBuffer.put(str.getBytes()); // 反转, read -> write
byteBuffer.flip(); // 将buffer中的数据写入到file channel
fileChannel.write(byteBuffer); fileChannel.close();
}
}
2. 读文件操作
/**
* 读文件
*/
public class FileChannelTest02 {
public static void main(String[] args) throws Exception {
File file = new File("D://nio.txt");
// 创建一个输入流
FileInputStream fileInputStream = new FileInputStream(file);
// 得到file channel
FileChannel fileChannel = fileInputStream.getChannel();
// 创建一个缓存buffer
ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
// 将file channel 中的数据读入到buffer中
fileChannel.read(byteBuffer);
byte[] array = byteBuffer.array();
String data = new String(array);
System.out.println(data);
fileChannel.close();
}
}
3. 使用FileChannel拷贝文件
/**
* 文件的拷贝,用一个buffer完成的读写
*/
public class CopyFileTest03 {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("src/1.txt");
FileChannel channel = fileInputStream.getChannel(); FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
FileChannel channel1 = fileOutputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024);
/**
* 下面的代码存在bug , 只会读写一次,如果文件内容大于1024, 后面的内容就会丢失
*/
// // 将channel中的数据读到buffer中
// channel.read(buffer);
// buffer.flip();
// // 将缓冲区的数据读取到通道中
// channel1.write(buffer); while (true) {
// 如果调用clear()方法,会出现死循环, read == 0 .
buffer.clear();
int read = channel.read(buffer);
System.out.println(read);
if (read == -1) {
break;
}
// 数据从buffer中写到channel中
buffer.flip();
channel1.write(buffer);
}
fileInputStream.close();
fileOutputStream.close(); }
}
4. 调用FileChannel的API完成文件拷贝
/**
* 使用transferFrom 和 transferTo 拷贝文件
*
*/
public class CopyFileTest04 {
public static void main(String[] args) throws Exception {
FileInputStream fileInputStream = new FileInputStream("1.jpg");
FileChannel inputStreamChannel = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream("2.jpg");
FileChannel outputStreamChannel = fileOutputStream.getChannel();
// 文件拷贝
// outputStreamChannel.transferFrom(inputStreamChannel, 0, inputStreamChannel.size());
inputStreamChannel.transferTo(0, inputStreamChannel.size(), outputStreamChannel);
// 关闭通道和流
fileInputStream.close();
fileOutputStream.close(); }
}
注意:
读写同一个buffer时,需要flip();
读写1次buffer之后,需要clear(), 将buferr复位
NIO之FileChannel操作示例的更多相关文章
- NIO之Buffer操作示例
1. buffer常规操作 略 2. 只读buffer /** * 只读buffer */ public class BufferTest01 { public static void main(St ...
- Java基础知识强化之IO流笔记78:NIO之 FileChannel
Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 1. 打开FileChannel 在 ...
- C#文件的拆分与合并操作示例
C#文件的拆分与合并操作示例代码. 全局变量定义 ;//文件大小 //拆分.合并的文件数 int count; FileInfo splitFile; string splitFliePath; Fi ...
- java-redis集合数据操作示例(三)
redis系列博文,redis连接管理类的代码请跳转查看<java-redis字符类数据操作示例(一)>. 一.集合类型缓存测试类 public class SetTest { /** * ...
- java-redis列表数据操作示例(二)
接上篇博文<java-redis字符类数据操作示例(一)>,redis连接管理类的代码请跳转查看. 一.列表类型缓存测试类 public class ListTest { /** * 主测 ...
- 文件操作示例脚本 tcl
linux 下,经常会对用到文件操作,下面是一个用 tcl 写的文件操作示例脚本: 其中 set f01 [open "fix.tcl" w] 命令表示 打开或者新建一个文件“fi ...
- phpExcel 操作示例
片段 1 片段 2 phpExcel 操作示例 <?php //写excel //Include class require_once('Classes/PHPExcel.php'); requ ...
- Go interface 操作示例
原文链接:Go interface操作示例 特点: 1. interface 是一种类型 interface 是一种具有一组方法的类型,这些方法定义了 interface 的行为.go 允许不带任何方 ...
- Hudi 数据湖的插入,更新,查询,分析操作示例
Hudi 数据湖的插入,更新,查询,分析操作示例 作者:Grey 原文地址: 博客园:Hudi 数据湖的插入,更新,查询,分析操作示例 CSDN:Hudi 数据湖的插入,更新,查询,分析操作示例 前置 ...
随机推荐
- 添加环境变量(path)
使用命令提示符((cmd)(批处理)(Batch)(.bat))添加环境变量 永久环境变量 命令提示符下修改 ==注意:要使用管理员身份运行cmd== set PATH=%PATH%;要添加的路径 r ...
- MDX入门(2)
MDX语言对大小写不敏感,而对于查询轴来说简单有Columns,Rows,Pages,Chapters,Sections分别也可以用AXIS(0),AXIS(1),AXIS(2),AXIS(3),AX ...
- 【ABAP系列】SAP 业务界面同时显示KEY和文本
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 业务界面同时显示KEY和 ...
- C++笔记(2)——一些语法基础知识以及基本算法知识
今天和PAT无直接相关的关系,主要是关于一些语法/算法的笔记,因为我发现自己的基础还没有打扎实,有些时候看别人的代码还会觉得一头雾水,不明白代码的含义. 一些C/C++语法 先从语法开始吧.这部分很琐 ...
- 剑指offer--day03
1.1题目:斐波那契数列:大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0).n<=39 1.2解题思路:斐波那契数列公式为: 这道题递归很好写 ...
- SpringBoot使用webservice
Pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- MySQL的练习
mysql登录:方法1:使用Command Line Client登录,缺点:不显示报错信息 方法2:使用cmd登录(cmd的常用:查看ip地址:ipcongfi-----定时关机:shutdown ...
- Redis--小小总结
1.基本定义 memcached是纯粹的key-value内存数据库,也可能不应该叫数据库,应该叫另类缓存技术: Redis是一个基于内存的高性能key-value数据库:将数据全部加载到内存中,并定 ...
- vue2.0--axios的跨域问题
鉴于这个问题,特地的提取了一个demo来进行截图说明. 一.根据官网的的安装流程安装vue-cli # 全局安装 vue-cli $ npm install --global vue-cli # 创建 ...
- HDU-2189来生一起走
题目: 今天,又来了n位志愿者,指挥部需要将他们分为若干个小组,小组的数量不限,但是要求每个小组的人数必须为素数,请问我们有几种分组的方法呢? 特别说明: 1.可以只有一个组: 2.分组的方法只和人数 ...