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 数据湖的插入,更新,查询,分析操作示例 前置 ...
随机推荐
- (转)sqlite developer注册方法
本文转载自:http://blog.csdn.net/fm0517/article/details/7912525 删除注册表中HKEY_CURRENT_USER\SharpPlus\SqliteDe ...
- Redis 简介,安装,卸载
一.Redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...
- 用Vue来实现音乐播放器(十八):右侧快速入口点击高亮
问题一:当我们点击右侧快速入口的时候 被点击的地方高亮 首先我们要知道右侧快速入口是为什么高亮??因为当watch()监控到scrollY的变化了的时候 将scrollY的值和listHeight ...
- kafka 配置权限
参考:https://www.cnblogs.com/huxi2b/p/10437844.html http://kafka.apache.org/documentation/#security_au ...
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_04 数据结构_5_数据结构_红黑树
生活中的树和计算机中的树.计算机的树是倒着的
- 阶段1 语言基础+高级_1-3-Java语言高级_04-集合_06 Set集合_2_哈希值
没有重写就是十进制的整数,重写了想返回多少就是多少. 创建Person类,没有写继承关系,默认会继承Object类 打开Object这个类 找到HashCode这个方法.就这一行代码.甚至都没有方法体 ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_12_练习_文件复制
谁用字节可以读取任何文件. 读取后再写入到D盘去.这就是一个文件的复制. 怎么由C盘到D盘.这里要借助流技术 需要创建两个流,一个读取一个写入 图片复制过来了. 看一下图片的字节数.whilte循环要 ...
- 佳能mp288拆解步骤--绝对原创
http://itbbs.pconline.com.cn/office/50663206.html 佳能mp288拆解步骤--绝对原创 gotobug Lv1太平洋舰队新兵 楼主 2013-10-13 ...
- Oracle删除表时候有外键 不能删除
SELECT A .constraint_name, A .table_name, b.constraint_nameFROM user_constraints A, u ...
- pyenv python 版本控制
经常遇到这样的情况: 系统自带的Python是2.x,自己需要Python 3.x,此时需要在系统中安装多个Python,但又不能影响系统自带的Python,即需要实现Python的多版本共存,pye ...