channel代码示例
来源:互联网 发布:亚洲人讲英语 知乎 编辑:程序博客网 时间:2024/06/05 10:14
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 { // 分散和聚集 @Test public 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 */ @Test public 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 */ @Test public 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(); } @Test public 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(); }}
0 0
- channel代码示例
- Go channel 使用示例代码
- 微软一站式示例代码库Mei Liang对话Channel 9 著名主持人Robert Green - 介绍一站式示例代码浏览器
- 微软一站式示例代码库Mei Liang对话Channel 9 著名主持人Robert Green - 介绍一站式示例代码浏览器
- event-channel代码分析
- Channel
- channel
- channel
- Channel
- channel
- Channel
- Channel
- Dark Channel Prior的Matlab代码
- 代码示例
- 代码示例
- 示例代码
- 示例代码
- Channel 9上的两个新Windows 8 示例视频
- 无脑利用gsettings修改截图默认保存位置
- Spring MVC 的@RequestParam注解和request.getParameter("XXX")
- java基础知识(五)
- 测试图片
- Strlen与Strcpy的模拟实现
- channel代码示例
- linux下安装pygame错误:linux/videodev.h:No such file or directory error解决方法
- (枚举,dfs)The Pilots Brothers' refrigerator poj 2965
- 【mark,计划2017年底开始用Linux~】完全用 Gnu/Linux 工作
- Centos7 配置静态IP
- 【C++学习笔记】对float型数据类型和double型数据的理解
- Linux下安装mysql数据库
- openGL学习(二)
- 消息通知之Notification与PendingIntent<一>