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操作示例的更多相关文章

  1. NIO之Buffer操作示例

    1. buffer常规操作 略 2. 只读buffer /** * 只读buffer */ public class BufferTest01 { public static void main(St ...

  2. Java基础知识强化之IO流笔记78:NIO之 FileChannel

    Java NIO中的FileChannel是一个连接到文件的通道.可以通过文件通道读写文件. FileChannel无法设置为非阻塞模式,它总是运行在阻塞模式下. 1. 打开FileChannel 在 ...

  3. C#文件的拆分与合并操作示例

    C#文件的拆分与合并操作示例代码. 全局变量定义 ;//文件大小 //拆分.合并的文件数 int count; FileInfo splitFile; string splitFliePath; Fi ...

  4. java-redis集合数据操作示例(三)

    redis系列博文,redis连接管理类的代码请跳转查看<java-redis字符类数据操作示例(一)>. 一.集合类型缓存测试类 public class SetTest { /** * ...

  5. java-redis列表数据操作示例(二)

    接上篇博文<java-redis字符类数据操作示例(一)>,redis连接管理类的代码请跳转查看. 一.列表类型缓存测试类 public class ListTest { /** * 主测 ...

  6. 文件操作示例脚本 tcl

    linux 下,经常会对用到文件操作,下面是一个用 tcl 写的文件操作示例脚本: 其中 set f01 [open "fix.tcl" w] 命令表示 打开或者新建一个文件“fi ...

  7. phpExcel 操作示例

    片段 1 片段 2 phpExcel 操作示例 <?php //写excel //Include class require_once('Classes/PHPExcel.php'); requ ...

  8. Go interface 操作示例

    原文链接:Go interface操作示例 特点: 1. interface 是一种类型 interface 是一种具有一组方法的类型,这些方法定义了 interface 的行为.go 允许不带任何方 ...

  9. Hudi 数据湖的插入,更新,查询,分析操作示例

    Hudi 数据湖的插入,更新,查询,分析操作示例 作者:Grey 原文地址: 博客园:Hudi 数据湖的插入,更新,查询,分析操作示例 CSDN:Hudi 数据湖的插入,更新,查询,分析操作示例 前置 ...

随机推荐

  1. axios的详细用法以及后端接口代理

    安装 使用 npm: $ npm install axios 或者 使用 bower: $ bower install axios 或者直接使用 cdn: <script src="h ...

  2. 阶段1 语言基础+高级_1-3-Java语言高级_07-网络编程_第1节 网络通信概述_5_端口号

  3. python读写ini配置文件

    像邮箱等信息是可以写在配置文件里面的,python有一个配置模块ConfigParser,可以处理配置文件信息 目录 1.配置模块ConfigParser 2.基本应用 1.配置模块ConfigPar ...

  4. Swiper轮播隐藏再显示后不动

    公告用Swiper轮播方式,在某些不需要显示公告的页面进行隐藏,在需要展示公告的页面进行显示时候,公告不能正常轮播,在条件里加入重新设置轮播方法等网上的一些方法仍然不行,最后解决方法: this.my ...

  5. python 正则表达式 re.sub & re.subn

    Grammar: re.sub(pattern, repl, string[, count]) 使用repl替换string中每一个匹配的子串后返回替换后的字符串.当repl是一个字符串时,可以使用\ ...

  6. MYSQL5.5 linux 多实例

    安装过程 cmake 安装参照上一篇 https://www.cnblogs.com/lixuchun/p/9240888.html 多实例采用 /data 目录作为mysql多实例的总的根目录,然后 ...

  7. java锁的概念

    在学习或者使用Java的过程中进程会遇到各种各样的锁的概念:公平锁.非公平锁.自旋锁.可重入锁.偏向锁.轻量级锁.重量级锁.读写锁.互斥锁等待.这里整理了Java中的各种锁,若有不足之处希望大家在下方 ...

  8. Web高级 JavaScript中的算法

    算法 排序算法 稳定排序 待排序序列中相等元素在排序完成后,原有先后顺序不变. 非稳定排序 有序度 待排序序列中有序关系的元素对个数. 逆序度 1. 插入排序 遍历有序数组,对比待插入的元素大小,找到 ...

  9. 使用lua实现try-catch异常捕获

    lua原生并没有提供try-catch的语法来捕获异常处理,但是提供了pcall/xpcall等接口,可在保护模式下执行lua函数. 因此,可以通过封装这两个接口,来实现try-catch块的捕获机制 ...

  10. 《剑指offer》面试题14 调整数组顺序使奇数位于偶数前面 Java版

    (输入整数数组,使所有奇数位于前半部分,所有偶数位于后半部分.) 我的方法:想到用两个下标分别表示奇数和偶数的界线,一个在开头,一个在末尾,判断每一个数字的类别,然后将它放入对应的范围内,移动下标,直 ...