channel代码示例

package cn.com.github.immortals;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;import org.junit.Test;/** * * @author 潘琢文 * */
public class TestChannel {// 分散和聚集@Testpublic void test4() throws IOException {RandomAccessFile raf1 = new RandomAccessFile("1.txt", "rw");// 1. 获取通道FileChannel channel1 = raf1.getChannel();// 2. 分配指定大小的缓冲区ByteBuffer buf1 = ByteBuffer.allocate(100);ByteBuffer buf2 = ByteBuffer.allocate(1024);// 3. 分散读取ByteBuffer[] bufs = { buf1, buf2 };channel1.read(bufs);for (ByteBuffer byteBuffer : bufs) {byteBuffer.flip();}System.out.println(new String(bufs[0].array(), 0, bufs[0].limit()));System.out.println("-----------------");System.out.println(new String(bufs[1].array(), 0, bufs[1].limit()));// 4. 聚集写入RandomAccessFile raf2 = new RandomAccessFile("2.txt", "rw");FileChannel channel2 = raf2.getChannel();channel2.write(bufs);}/** * 利用channel复制文件 * * @throws IOException */@Testpublic void testChannel() throws IOException {FileInputStream ins = new FileInputStream(new File("D:/大话西游之大圣娶亲[国语版]_bd.mp4"));FileOutputStream fos = new FileOutputStream(new File("D:/大话西游之大圣娶亲[国语版]_bd_copy.mp4"));FileChannel in = ins.getChannel();FileChannel out = fos.getChannel();ByteBuffer buf = ByteBuffer.allocate(1024);while (in.read(buf) != -1) {buf.flip();out.write(buf);buf.clear();}ins.close();fos.close();in.close();out.close();}/** * channel的直接缓冲区复制文件 * * @throws IOException */@Testpublic void testChannel2() throws IOException {FileChannel in = FileChannel.open(Paths.get("D:/大话西游之大圣娶亲[国语版]_bd.mp4"), StandardOpenOption.READ);FileChannel out = FileChannel.open(Paths.get("D:/大话西游之大圣娶亲[国语版]_bd_copy.mp4"), StandardOpenOption.CREATE,StandardOpenOption.WRITE);in.transferTo(0, in.size(), out);in.close();out.close();}@Testpublic void testChannel3() throws IOException {FileChannel in = FileChannel.open(Paths.get("D:/大话西游之大圣娶亲[国语版]_bd.mp4"), StandardOpenOption.READ);FileChannel out = FileChannel.open(Paths.get("D:/大话西游之大圣娶亲[国语版]_bd_copy.mp4"), StandardOpenOption.CREATE,StandardOpenOption.WRITE, StandardOpenOption.READ);MappedByteBuffer inBuffer = in.map(MapMode.READ_ONLY, 0, in.size());MappedByteBuffer outBUffer = out.map(MapMode.READ_WRITE, 0, in.size());byte[] dst = new byte[inBuffer.limit()];inBuffer.get(dst);outBUffer.put(dst);in.close();out.close();}
}

转载于:https://my.oschina.net/ikiru/blog/1536634

channel代码示例相关推荐

  1. 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 完整代码示例 )

    文章目录 前言 一.Android 端完整代码示例 二.Flutter 端完整代码示例 三.相关资源 前言 前置博客 : [Flutter]Flutter 混合开发 ( Flutter 与 Nativ ...

  2. java网络编程阻塞_Java网络编程由浅入深三 一文了解非阻塞通信的图文代码示例详解...

    本文详细介绍组成非阻塞通信的几大类:Buffer.Channel.Selector.SelectionKey 非阻塞通信的流程ServerSocketChannel通过open方法获取ServerSo ...

  3. pythoninterp error_Python numpy.interp方法代码示例

    本文整理汇总了Python中numpy.interp方法的典型用法代码示例.如果您正苦于以下问题:Python numpy.interp方法的具体用法?Python numpy.interp怎么用?P ...

  4. RabbitMq(七) Topic模式介绍及代码示例

    概述: 在上一文章中我们介绍了路由模式(Routing),routing模式是不同的消息队列绑定了不同的路由key,但是我们看出路由key为固定的字符串标记.而本章中的Topic模式则为在路由模式下, ...

  5. Netty框架多人聊天案例,代码示例

    Netty框架多人聊天案例,代码示例 pom <?xml version="1.0" encoding="UTF-8"?> <project ...

  6. BIO,Socket网络编程入门代码示例,NIO网络编程入门代码示例,AIO 网络编程

    BIO,Socket网络编程入门代码示例 1.BIO服务器端程序 package cn.itcast.bio;import java.io.InputStream; import java.io.Ou ...

  7. python batch_size_Python config.batch_size方法代码示例

    本文整理汇总了Python中config.batch_size方法的典型用法代码示例.如果您正苦于以下问题:Python config.batch_size方法的具体用法?Python config. ...

  8. python3的fft_Python fft.fft方法代码示例

    本文整理汇总了Python中numpy.fft.fft方法的典型用法代码示例.如果您正苦于以下问题:Python fft.fft方法的具体用法?Python fft.fft怎么用?Python fft ...

  9. python中uppercase是什么意思_Python string.ascii_uppercase方法代码示例

    本文整理汇总了Python中string.ascii_uppercase方法的典型用法代码示例.如果您正苦于以下问题:Python string.ascii_uppercase方法的具体用法?Pyth ...

  10. python关联通达信pywin32_Python ctypes.wintypes方法代码示例

    本文整理汇总了Python中ctypes.wintypes方法的典型用法代码示例.如果您正苦于以下问题:Python ctypes.wintypes方法的具体用法?Python ctypes.wint ...

最新文章

  1. 从零搭建一套结构光3D重建系统[理论+源码+实践]
  2. 机器学习--多标签softmax + cross-entropy交叉熵损失函数详解及反向传播中的梯度求导
  3. iptables做路由转发服务器经典案例
  4. Cisco 3550配置DHCP的实际经验
  5. boot loader能全部用C程序编写吗
  6. vfp操作excel排序_中招计算机信息技术考试训练|Excel操作题一|排序和筛选
  7. python 访问网页aspx_asp.net – 如何向python中的.aspx页面提交查询
  8. php中如果想要打印出来的结果换行怎么操作_现在,就要对文件的批操作,动刀子啦!!!...
  9. jQuery学习笔记:文档处理
  10. 张俊芳电机学13章计算题以及答案
  11. 入门Sysmac Studio,白菜妹子是这样做的。
  12. 电脑上有哪些特别好用的小工具?盘点4个PC工具,个个都精品
  13. 三十四、Expandable grid 可扩展的表格
  14. java使用oshi获取硬件信息,包括cpu温度
  15. 计算机专业毕业生的就业政策,计算机专业毕业生就业情况分析及应对策略
  16. BZOJ4049][CERC2014]Mountainous landscape-[线段树+凸包+二分]
  17. 【Java项目】好客租房——数据库集群部署
  18. 基于PHP+MySQL动漫周边商城销售网站的开发与设计
  19. C语言例题——简易计算器
  20. scratch下实现子弹追踪特效

热门文章

  1. 硬盘文件系统系列之FAT
  2. SD卡驱动-基础知识
  3. python基础课件
  4. 光纤光缆那些纤常识【华光昱能光知识】
  5. 【Unity Shaders】ShadowGun系列之二——雾和体积光
  6. SREng 使用指南(五)扩展的解说
  7. 远程网络教学系统用例图
  8. 详解动态代理及其实现原理
  9. LabVIEW与Microsoft Windows的兼容性
  10. HTC G14解锁S-OFF、刷机、获取ROOT权限