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. (转)SQLite部署-无法加载 DLL“SQLite.Interop.dll”: 找不到指定的模块

    本文转载自:http://www.cnblogs.com/muzhiye/p/4284070.html 近期刚使用SQLite,主要引用的是System.Data.SQLite.dll这个dll,在部 ...

  2. HTML - form 表单提交

    form 表单提交 数据发送 disabled:不发送 display_none:发送 type_hidden:发送 readonly:发送 测试 html: <!DOCTYPE html> ...

  3. python使用__new__创建一个单例模式(单例对象)

    #单例模式:使一个类只产生一个对象.他们的id地址都指向同一个内存地址 第一步:理解谁创建了对象 # 单例模式# 首先明白,我们在创建一个类的对象的时候,其实是调用的这个类的父类,即继承object, ...

  4. SQL*Plus 与数据库的交互

    设置SQL *Plus的运行环境 SET 命令格式: set system_variable value pagesize :从顶部标题到页结束之间的行数 默认是14 newpage:一页中空行的数量 ...

  5. 《图解设计模式》读书笔记9-2 Proxy模式

    目录 Proxy模式 示例程序 程序描述 类图 程序 角色和类图 角色 模式类图 思路拓展 提升速度 代理与委托 Http代理 与其他模式的关联 Decorator模式 Proxy模式 Proxy是代 ...

  6. docker进阶——数据管理与网络

    一.数据卷管理 用户在使用 Docker 的过程中,势必需要查看容器内应用产生的数据,或者 需要将容器内数据进行备份,甚至多个容器之间进行数据共享,这必然会涉及 到容器的数据管理 (1)Data Vo ...

  7. 从DT时代云栖大会聊聊恒生电子

    从IT到DT,除了HOMS和配资,本文结合互联网金融的背景,帮助读者对恒生电子600570了解更多. 马云在2015杭州·云栖大会Computing Conference发表致辞时多次强调DT(Dat ...

  8. 【MM系列】SAP 主要模块及简介

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 主要模块及简介   前言部分 ...

  9. linux优化

    优化linux启动项 1. 使用ntsysv工具讲不需要的服务关闭 2. 默认启动服务可以只保留必要的服务 3. free -m 以m为单位 4. 删除不必要的用户: 5. cp /etc/passw ...

  10. 获取文件夹中前N个文件

    @echo off set input="list.txt" set srcDir="%1" set /a fileCount=10 set /a curInd ...