一、通道(Channel):由 java.nio.channels 包定义的。Channel 表示 IO 源与目标打开的连接。Channel 类似于传统的“流”。只不过 Channel本身不能直接访问数据,Channel 只能与Buffer 进行交互。

  二、Channel重要实现

  • FileChannel:操作文件的读写
  • SocketChannel:通过TCP读写网络数据
  • ServerSocketChannel:监听TCP连接,你能利用它创建一个最简单的Web服务器
  • DatagramChannel:通过UDP读写网络数据

  三、FileChannel 的文件读写

  1)利用FileChannel 本身提供的transferTo进行数据的读写。

package com.troy.nio.application;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel; public class Channel { public static void main(String[] args) throws Exception {
//读取文件
FileInputStream fileInputStream = new FileInputStream("d:/t.txt");
//写出文件
FileOutputStream fileOutputStream = new FileOutputStream("d:/e.txt");
//获取读取通道
FileChannel inChannel = fileInputStream.getChannel();
//获取写入通道
FileChannel outChannel = fileOutputStream.getChannel();
//完成数据的写入
inChannel.transferTo(,inChannel.size(),outChannel);
}
}

  2)利用FileChannel 提供的读写方法

package com.troy.nio.application;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class Channel { public static void main(String[] args) throws Exception {
//读取文件
FileInputStream fileInputStream = new FileInputStream("d:/t.txt");
//写出文件
FileOutputStream fileOutputStream = new FileOutputStream("d:/e.txt");
//获取读取通道
FileChannel inChannel = fileInputStream.getChannel();
//获取写入通道
FileChannel outChannel = fileOutputStream.getChannel();
//缓存
ByteBuffer byteBuffer = ByteBuffer.allocate();
//读取数据
while (inChannel.read(byteBuffer) != -) {
//转换成可读写
byteBuffer.flip();
System.out.println(new String(byteBuffer.array(),"GBK").trim());
//写出数据,清楚缓存
outChannel.write(byteBuffer);
byteBuffer.clear();
}
}
}

  四、SocketChannel和ServerSocketChannel在同时使用时,都是tcp协议进行传输的,在使用上面比较服务具体的协议控制

  具体的应用可以参考:http://www.cnblogs.com/ll409546297/p/7929646.html

  五、DatagramChannel的方式

  1)客户端

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel; public class UDPClient { public static void main(String[] args) throws Exception { //获取UDP通道
DatagramChannel datagramChannel = DatagramChannel.open();
//设置非阻塞
datagramChannel.configureBlocking(false);
//发送数据
datagramChannel.send(ByteBuffer.wrap("hello server!".getBytes()),new InetSocketAddress("localhost",));
}
}

  2)服务端的2中写法,阻塞和非阻塞

  1、阻塞

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel; public class UDPServer { //UDP通道
private static DatagramChannel datagramChannel; public static void main(String[] args) throws Exception {
serverInit();
listen();
} //初始化
private static void serverInit() throws IOException {
//获取UDP通道
datagramChannel = DatagramChannel.open();
//设置接收端口
datagramChannel.socket().bind(new InetSocketAddress());
} //监听
private static void listen() throws IOException {
while (true) {
//接收的长度
ByteBuffer byteBuffer = ByteBuffer.allocate();
//这里会阻塞
datagramChannel.receive(byteBuffer);
byteBuffer.flip();
System.out.println(new String(byteBuffer.array()).trim());
}
}
}

  2、非阻塞,利用selector来进行数据选择

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator; public class UDPServer { //选择器
private static Selector selector;
//UDP通道
private static DatagramChannel datagramChannel; public static void main(String[] args) throws Exception {
serverInit();
listen();
} //初始化
private static void serverInit() throws IOException {
//获取选择器
selector = Selector.open();
//获取UDP通道
datagramChannel = DatagramChannel.open();
//设置非阻塞
datagramChannel.configureBlocking(false);
//设置接收端口
datagramChannel.socket().bind(new InetSocketAddress());
//注册
datagramChannel.register(selector, SelectionKey.OP_READ);
} //监听
private static void listen() throws IOException {
while (true) {
selector.select();
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey selectionKey = iterator.next();
if (selectionKey.isReadable()) {
//接收的长度
ByteBuffer byteBuffer = ByteBuffer.allocate();
//这里不会阻塞
datagramChannel.receive(byteBuffer);
byteBuffer.flip();
System.out.println(new String(byteBuffer.array()).trim());
}
}
}
}
}

  六、基本上channel的实现用法就这些了,但是里面会涉及到很多细节的用法,这个需要自己进一步研究

java nio之channel的更多相关文章

  1. Java NIO -- 通道 Channel

    通道(Channel):由 java.nio.channels 包定义的.Channel 表示 IO 源与目标打开的连接.Channel 类似于传统的“流”.只不过 Channel本身不能直接访问数据 ...

  2. Java NIO 之 Channel(通道)

    历史回顾: Java NIO 概览 Java NIO 之 Buffer(缓冲区) 其他高赞文章: 面试中关于Redis的问题看这篇就够了 一文轻松搞懂redis集群原理及搭建与使用 一 Channel ...

  3. 【Java nio】Channel

    package com.slp.nio; import org.junit.Test; import java.io.*; import java.nio.ByteBuffer; import jav ...

  4. JAVA NIO 之Channel

    缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.Channel 通道就是将数据传输给 ByteBuffer 对象或者从 ByteBuffer 对象获取数据进行传输. Channel 用于在 ...

  5. Java NIO教程 Channel

    Channel是一个连接到数据源的通道.程序不能直接用Channel中的数据,必须让Channel与BtyeBuffer交互数据,才能使用Buffer中的数据. 我们用FileChannel作为引子, ...

  6. 《JAVA NIO》Channel

    3.通道 Channle主要分为两类:File操作对应的FIleChannel和Stream操作对应的socket的3个channe. 1.这3个channel都是抽象类.其具体实现在SPI里面. 2 ...

  7. (转)[疯狂Java]NIO:Channel的map映射

    原文出自:http://blog.csdn.net/lirx_tech/article/details/51396268 1. 通道映射技术: 1) 其实就是一种快速读写技术,它将通道所连接的数据节点 ...

  8. 关于java nio的channel读写的一个困惑

    这里提的需求基本都是IM的,IM的解决方案是怎么样的? 网上的需求: 1. 某一用户发了一条信息, 需要服务器反回一个信息(这种最简单) 2. 某一用户发了一条信息,需要服务器广播给所有客户端 3. ...

  9. JAVA NIO Selector Channel

    These four events are represented by the four SelectionKey constants: SelectionKey.OP_CONNECT Select ...

随机推荐

  1. aop的概念以及 cglib-nodep-2.1_3.jar第三方jia包动态代理使用

    引入 cglib-nodep-2.1_3.ja包 cglib产生的代理类是目标类的子类 定义接口,让切面都继承它,方便加入到动态代理方法 的那个类中使用 在SalaryInterceptor类中使用  ...

  2. 在CentOS实现mysql数据库的自动备份

    数据是一个比较重要的数据,经常需要备份,每次都手动比较麻烦.本脚本主要现实在CentOS中实现对数据库的备份和保留最近十五天的备份文件.避免太多无用陈旧的备份占用空间. #!/bin/bashid=& ...

  3. Powershell split file

    #网上所得非原创 function split($inFile, $outPrefix, [Int32] $bufSize){ $stream = [System.IO.File]::OpenRead ...

  4. BZOJ1116:[POI2008]CLO(并查集)

    Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 你要把其中一些road变成单向边使得:每个t ...

  5. luogu3368树状数组模板2

    题目链接:https://www.luogu.org/problemnew/show/P3368 题意:与模板1不同的是这题的操作是树状数组并不在行的区间更新和单点查找,如果按照模板1那样写肯定会T. ...

  6. ASP.NET SingalR 点对点聊天实现思路总结

    前一段时间写了一个简单的聊天室,是群聊的方式.博客地址:http://www.cnblogs.com/panzi/p/4980346.html.还有一种需求就是常见的尤其是培训机构的主页面,经常会有1 ...

  7. tomcat部署web项目方法

    一.tomcat部署web项目之静态配置: 方法一:直接将为web项目放到webapps下: 如图所示:在webapps文件下,存放了一个html文件,启动tomcat,访问http://localh ...

  8. C#中Form的Paint事件响应方法与重载虚方法OnPaint()的区别

    Form_Paint()方法是Paint事件的响应方法,OnPaint是可重载的虚方法,OnPaint方法是调用Paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的: protec ...

  9. 用条件变量实现事件等待器的正确与错误做法--转自陈硕的Blog

    用条件变量实现事件等待器的正确与错误做法 TL;DR 如果你能一眼看出 https://gist.github.com/chenshuo/6430925 中的那 8 个 Waiter classes ...

  10. 单独调用kindeditor的多图上传组件实现多图上传

    本例是单独调用kindeditor多图上传的组件来进行多图上传,兼容性你懂得! 官方示例地址:http://kindeditor.net/ke4/examples/multi-image-dialog ...