【Java nio】Channel
package com.slp.nio; import org.junit.Test; import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;
import java.util.Set; /**
* Created by sanglp on 2017/3/1.
* 一、通道:用于源节点与目标节点的连接,在Java NIO中负责缓冲区中数据的传输。通道本身是不存储任何数据的,因此需要配合缓冲区进行传输数据
* 二、通道的一些主要实现类
* java.nio.Channel接口
* |--FileChannel
* |--SocketChannel
* |--ServerSocketChannel
* |--DatagramChannel
* 三、获取通道
* 1.java针对支持通道的类提供了getChannel()方法
* 本地IO:
* FileInputStream/FileOutputStream/RandomAccessFile
* 网络IO:
* Socket
* ServerSocket
* DategramSocket
* 2.在jdk1.7中的NIO2针对各个通道提供了一个静态方法open()
* 3。jdk1.7中的NIO2的Files工具类的newByteChannel()
* 四、通道之间的数据传输
* transferFrom()
* transferTo()
* 五、分散和聚集
* 分散度区:将通道中的数据分散到多个缓冲区中
* 聚集写入:将多个缓冲区中的数据聚集到通道中
*
* 六、字符集:Charset
* 编码:字符串->字节数组
* 解码:字节数组->字符串
*/
public class TestChannel {
@Test
public void test6() throws CharacterCodingException {
Charset cs1 = Charset.forName("GBK");
//获取编码器和解码器
CharsetEncoder ce = cs1.newEncoder();
//获取解码器
CharsetDecoder cd = cs1.newDecoder(); CharBuffer charBuffer = CharBuffer.allocate(1024);
charBuffer.put("桑丽平加油!!");
charBuffer.flip(); //编码
ByteBuffer byteBuffer = ce.encode(charBuffer);
for (int i=0;i<12;i++){
System.out.println(byteBuffer.get(i));
} //解码
byteBuffer.flip();
CharBuffer charBuffer1 = cd.decode(byteBuffer);
System.out.println(charBuffer1.toString()); System.out.println("------------------------"); Charset cs2 = Charset.forName("UTF-8");
byteBuffer.flip();
CharBuffer cBuf3 =cs2.decode(byteBuffer);
System.out.println(cBuf3.toString());
} @Test
public void test5(){
Map<String,Charset> map=Charset.availableCharsets();
Set<Map.Entry<String,Charset>> set = map.entrySet();
for (Map.Entry<String,Charset> entry :set ){
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
@Test
public void test4() throws IOException {
RandomAccessFile randomAccessFile = new RandomAccessFile("1.txt","rw");
//获取通道
FileChannel channel = randomAccessFile.getChannel();
//分配指定大小的缓冲区
ByteBuffer buffer1 = ByteBuffer.allocate(100);
ByteBuffer buffer2 = ByteBuffer.allocate(1024);
//分散读取
ByteBuffer [] bufs = {buffer1,buffer2};
channel.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())); //聚集写入
RandomAccessFile randomAccessFile1 = new RandomAccessFile("2.txt","rw");
FileChannel channel1 = randomAccessFile1.getChannel();
channel1.write(bufs); } //通道之间的数据传输(直接缓冲区)
@Test
public void test3() throws IOException {
FileChannel fileChannel = FileChannel.open(Paths.get("pipe.bmp"), StandardOpenOption.READ);
FileChannel outChannel =FileChannel.open(Paths.get("4.bmp"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW); fileChannel.transferTo(0,fileChannel.size(),outChannel);
//outChannel.transferFrom(fileChannel,0,fileChannel.size());或者
fileChannel.close();
outChannel.close();
} //2、利用直接缓冲区完成文件的复制(内存映射文件)
@Test
public void test2() throws IOException {
FileChannel fileChannel = FileChannel.open(Paths.get("pipe.bmp"), StandardOpenOption.READ);
FileChannel outChannel =FileChannel.open(Paths.get("3.bmp"),StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE_NEW);
//内存映射文件
MappedByteBuffer imMappedBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY,0,fileChannel.size());
MappedByteBuffer outMappedBuf = outChannel.map(FileChannel.MapMode.READ_WRITE,0,fileChannel.size());
//直接对缓冲区进行数据的读写操作
byte [] dst = new byte[imMappedBuf.limit()];
imMappedBuf.get(dst);
outMappedBuf.put(dst); fileChannel.close();
outChannel.close(); } //1、利用通道完成文件的复制(费直接缓冲区)
@org.junit.Test
public void test1(){
FileInputStream fis =null;
FileOutputStream fos =null;
FileChannel inChanne=null;
FileChannel outChannel=null;
try{
fis = new FileInputStream("pipe.bmp");
fos = new FileOutputStream("2.jpg");
//获取通道
inChanne =fis.getChannel();
outChannel = fos.getChannel();
//分配指定大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate(1024);
//将通道中的数据存入缓冲区中
while (inChanne.read(buffer)!=-1) {
buffer.flip();//切换为读取数据的模式
//将缓冲区中的数据写入通道
outChannel.write(buffer);
buffer.clear();//清空缓冲区
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(outChannel!=null){
try {
outChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inChanne!=null){
try {
inChanne.close();
} catch (IOException e) {
e.printStackTrace();
}
} if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }
}
【Java nio】Channel的更多相关文章
- 【Java nio】 NonBlocking NIO
package com.slp.nio; import org.junit.Test; import java.io.IOException; import java.net.InetSocketAd ...
- 【Java nio】 Blocking nio
package com.slp.nio; import org.junit.Test; import java.io.File; import java.io.IOException; import ...
- 【Java nio】java nio笔记
缓冲区操作:缓冲区,以及缓冲区如何工作,是所有I/O的基础.所谓“输入/输出”讲的无非就是把数据移出货移进缓冲区.进程执行I/O操作,归纳起来也就是向操作系统发出请求,让它要么把缓冲区里的数据排干,要 ...
- 【Java NIO】一文了解NIO
Java NIO 1 背景介绍 在上一篇文章中我们介绍了Java基本IO,也就是阻塞式IO(BIO),在JDK1.4版本后推出了新的IO系统(NIO),也可以理解为非阻塞IO(Non-Blocking ...
- 【JAVA NIO】java NIO
本文是博主深入学习Netty前的一些铺垫,之前只是使用Netty,用的很粗暴,导包,上网找个DEMO就直接用,对Netty中的组件了解并不深入. 于是再此总结下基础,并对一些核心组件作如下记录: 1. ...
- 【Java nio】Blocking nio2
package com.slp.nio; import org.junit.Test; import java.io.File; import java.io.IOException; import ...
- 【Java nio】buffer
package com.slp.nio; import org.junit.Test; import java.nio.ByteBuffer; /** * Created by sanglp on 2 ...
- 【java NIO】服务器端读写图片的一次排错经历
上传文件方面: 一.前端 使用的是jQuery框架来上传图片,参考的是harttle大神博客:http://harttle.com/2016/07/04/jquery-file-upload.html ...
- 【Java面试】基础知识篇
[Java面试]基础知识篇 Java基础知识总结,主要包括数据类型,string类,集合,线程,时间,正则,流,jdk5--8各个版本的新特性,等等.不足的地方,欢迎大家补充.源码分享见个人公告.Ja ...
随机推荐
- NLog 配置与使用
有段时间没写博客了,过年放假,一直在弄CMS.什么都自己写了一遍,今天写写NLog,之前一用的log4net,感觉配置起来还是有些麻烦. NuGet 添加组件 配置 NLog.config <? ...
- C++对析构函数的误解
C++析构前言 析构函数在什么时候会自动被调用,在什么时候需要手动来调用,真不好意思说偶学过C++…今日特此拨乱反正. C++析构误解正文 对象在构造的时候系统会分配内存资源,对一些数据成员进行初始化 ...
- 关于Cocos Creator用js脚本代码播放骨骼动画的步骤和注意事项
步骤: 1.用cc.find()方法找到相应的骨骼动画节点,并把这个对象赋值给一个var出来的新对象. 具体代码:var spineboy_anim = cc.find("UI_Root/a ...
- 关于Cocos2d-x中节点和精灵的关系以及初始化
1.每一个对象类都有一个自己public的一个create函数(等价于CREATE_FUNC),和init函数. 2.create函数返回的是自身的类型,init函数是在ceate函数被调用的时候自动 ...
- [转载] PHP开发必看 编程十大好习惯
适当抽象 但是在抽象的时候,要避免不合理的抽象,有时也可能造成过渡设计,现在只需要一种螺丝刀,但你却把更多类型的螺丝刀都做出来了(而且还是瑞士军刀的样子..): 一致性 团队开发中,可能每个人的编程风 ...
- PHP与ASP.NET的优劣比较
PHP与ASP.NET的比较 表 1 PHP 4 PHP5 ASP.NET 软件价格 免费 免费 免费 平台价格 免费 免费 $$ 速度 强 强 弱 效率 强 强 弱 安全性 强 强 强 平台 强 强 ...
- 以下哪个Hibernate主键生成策略是实现主键按数值顺序递增的?
A.increment B.identity C.sequence D.native 解答:A
- (转)c++多态实现的机制
原文地址:http://blog.csdn.net/zyq0335/article/details/7657465 1 什么是多态?多态性可以简单的概括为“1个接口,多种方法”,在程序运行的过程中才决 ...
- jquery -- onchange
触发onchange 首先页面有一个input标签,并且已绑定onchange事件,如: 1 <input type="text" onchange="consol ...
- Linux基础知识之history的详细说明
背景:history是Linux中常会用到内容,在工作中一些用户会突然发现其安装不了某个软件,于是寻求运维人员的帮助,而不给你说明他到底做了哪些坑爹的操作.此时你第一件要做的就是要查看其history ...