一:java的nio的channel

Java NIO的通道类似流,但又有些不同:

==>既可以从通道中读取数据,又可以写数据到通道。但流的读写通常是单向的。

==>通道可以异步地读写。

==>通道中的数据总是要先读到一个Buffer,或者总是要从一个Buffer中写入。

正如上面所说,从通道读取数据到缓冲区,从缓冲区写入数据到通道。如下图所示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVIAAADnCAIAAADDx0nKAAAAA3NCSVQICAjb4U/gAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAB3RFWHRBdXRob3IAqa7MSAAAAAh0RVh0Q29tbWVudAD2zJa/AAAACnRFWHRDb3B5cmlnaHQArA/MOgAAAA50RVh0Q3JlYXRpb24gdGltZQA19w8JAAAADHRFWHREZXNjcmlwdGlvbgATCSEjAAAAC3RFWHREaXNjbGFpbWVyALfAtI8AAAAJdEVYdFNvZnR3YXJlAF1w/zoAAAAHdEVYdFNvdXJjZQD1/4PrAAAABnRFWHRUaXRsZQCo7tInAAAACHRFWHRXYXJuaW5nAMAb5ocAACAASURBVHic7d17WFNXvjfwL7dAIAQEIYoWvFAUFbTU6nGmFW3FU7y+04tW7Wsrx+NjrUMdpNW25/TM2z5e6+Utj1Y6tU5fe1FGZzoWrU5jW7FMKzqiQr2Aj1WQqwhCBBJCstf7RwIEEBNDkr3D+n0e/th7kZX9I+xffmuvvXfiwRgDIYQnnmIHQAhxNUp7QrhDaU8IdyjtCeEOpT0h3KG0J4Q7lPaEcIfSnhDuUNoTwh1Ke0K4Q2lPCHco7QnhDqU9IdyhtCeEO5T2hHCH0p4Q7lDaE8IdSntCuENpTwh3KO0J4Q6lPSHcobQnhDuU9oRwh9KeEO5Q2hPCHUp7QrhDaU8IdyjtCeGOt9gBdGWoKzHWlhjqSoy1pWLH4hheoZHeIVGeSpWPaoTYsThLXv53129eFjsK9zD0odiJCU+JG4Mk0r61rECbf1BbkN1aXSR2LM7lHRrlF5vkFzfbb1SS2LE4TF7+dxt2rBQ7Cnfy5sod4ma+yGmvLcjW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5CYII=" alt="" />

二:java的nio的channel的实现

这些是Java NIO中最重要的通道的实现:

==>FileChannel  :

==>DatagramChannel

==>SocketChannel

==>ServerSocketChannel

三:java的nio的channel的实现的作用

==>FileChannel 从文件中读写数据。

==>DatagramChannel 能通过UDP读写网络中的数据。

==>SocketChannel 能通过TCP读写网络中的数据。

==>ServerSocketChannel可以监听新进来的TCP连接,像Web服务器那样。对每一个新进来的连接都会创建一个SocketChannel。

四:java的nio的channel的基本例子

 package com.yeepay.sxf.testnio;

 import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 简单测试java的nio中channel组件的作用
* @author sxf
*
*/
public class TestChannelForNio { public static void main(String[] args) throws IOException {
//把文件加载到内存 /usr/sxf/testnio文件
RandomAccessFile aFile = new RandomAccessFile("/usr/sxf/testnio", "rw");
//从文件中获取通道
FileChannel inChannel = aFile.getChannel();
//从通道中注册一个缓冲区,每次48个字节
ByteBuffer buf = ByteBuffer.allocate(48);
//读取第一次
int bytesRead = inChannel.read(buf);
//看文件中是否有数据
while (bytesRead != -1) {
//打印第一次读取字符的个数
System.out.println("Read " + bytesRead); //注意 buf.flip() 的调用,首先读取数据到Buffer,然后反转Buffer,接着再从Buffer中读取数据。
buf.flip(); //打印每一个字符到控制台
while(buf.hasRemaining()){
System.out.print((char) buf.get());
} //清空这次buffer
buf.clear(); bytesRead = inChannel.read(buf); } aFile.close();
}
}

java的nio之:java的nio系列教程之channel的概念的更多相关文章

  1. java的nio之:java的nio系列教程之channel的数据交换

    在Java NIO中,如果两个通道中有一个是FileChannel,那你可以直接将数据从一个channel(译者注:channel中文常译作通道)传输到另外一个channel. transferFro ...

  2. java的nio之:java的nio系列教程之buffer的概念

    一:java的nio的buffer==>Java NIO中的Buffer用于和NIO通道Channel进行交互.==>数据是从通道channel读入缓冲区buffer,从缓冲区buffer ...

  3. Spring 系列教程之 bean 的加载

    Spring 系列教程之 bean 的加载 经过前面的分析,我们终于结束了对 XML 配置文件的解析,接下来将会面临更大的挑战,就是对 bean 加载的探索.bean 加载的功能实现远比 bean 的 ...

  4. SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣

    在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...

  5. SpringBoot系列教程之Bean之指定初始化顺序的若干姿势

    上一篇博文介绍了@Order注解的常见错误理解,它并不能指定 bean 的加载顺序,那么问题来了,如果我需要指定 bean 的加载顺序,那应该怎么办呢? 本文将介绍几种可行的方式来控制 bean 之间 ...

  6. kali linux 系列教程之metasploit 连接postgresql可能遇见的问题

    kali linux 系列教程之metasploit 连接postgresql可能遇见的问题 文/玄魂   目录 kali linux 下metasploit 连接postgresql可能遇见的问题. ...

  7. kali Linux系列教程之BeFF安装与集成Metasploit

    kali Linux系列教程之BeFF安装与集成Metasploit 文/玄魂 kali Linux系列教程之BeFF安装与集成Metasploit 1.1 apt-get安装方式 1.2 启动 1. ...

  8. Kali Linux系列教程之OpenVas安装

    Kali Linux系列教程之OpenVas安装 文 /玄魂 目录 Kali Linux系列教程之OpenVas安装 前言 1.  服务器层组件 2.客户层组件 安装过程 Initial setup ...

  9. RabbitMQ系列教程之二:工作队列(Work Queues)(转载)

    RabbitMQ系列教程之二:工作队列(Work Queues)     今天开始RabbitMQ教程的第二讲,废话不多说,直接进入话题.   (使用.NET 客户端 进行事例演示)          ...

随机推荐

  1. MyQQ 前言

    从接触IOS以来也将近8个月了,学习了不少知识. 看了一系列的书籍如: <objective-c 开发入门>--语法介绍,学习一门语言是开发的基础.所以这是我大致对语法语句学习的锻炼地方, ...

  2. linux学习笔记4:linux的任务调度,进程管理,mysql的安装和使用,ssh工具的使用,linux网络编程

    1.设置任务调度命令crontab 任务调度是指系统在某个时间执行的特定的命令或程序.任务调度分为:1)系统工作:有些重要的工作必须周而复始的执行,如病毒扫描.2)个别用户工作:个别用户可能希望执行某 ...

  3. hdu 2045

    Ps:上课的时候用iPad做的...没有调试..一次AC机.就是简单的递推... 代码: #include "stdio.h"#include "math.h" ...

  4. 玩转无线电 -- GPS Hacking (上)

    0x00 序 GPS Hacking 在过去几年的安全会议上一直都是很受关注的议题. 但往往因为内容太过学术化, 所需设备成本太高. 让许多感兴趣的朋友苦于无法入门. 直到GPS-SDR-SIM 这类 ...

  5. IPhone手机自动添加到itunes设置

    一,项目设置 如图:点击项目--info 在key下面条目上右键点击,选择添加Application supports iTunes file sharing   value设置为yes

  6. 11、C#基础整理(特殊集合和哈希表)

    特殊集合:队列.栈 一.栈Stack类:先进后出,没有索引 Stack ss = new Stack(); 1.增加数据:push :将元素推入集合 ss.Push(); ss.Push(); ss. ...

  7. Selenium Grid原理

    转载: http://blog.csdn.net/five3/article/details/9428655 Selenium-Grid版本 selenium-grid分为版本1和版本2,其实它的2个 ...

  8. 如何实现Magento产品批量导入?

    从事外贸的我们在工作中,经常需要添加成千上万个的产品,如果一个一个的去上传,要花费很多时间,有是很让人头痛,那么应该如何实现产品批量上传?如果使用的是Magento系统的话,那么你现在有福利了,因为M ...

  9. Hooks

    function getValType(elem) { var ret = elem.tagName.toLowerCase() return ret === "input" &a ...

  10. PAT (Basic Level) Practise:1039. 到底买不买

    [题目链接] 小红想买些珠子做一串自己喜欢的珠串.卖珠子的摊主有很多串五颜六色的珠串,但是不肯把任何一串拆散了卖.于是小红要你帮忙判断一下,某串珠子里是否包含了全部自己想要的珠子?如果是,那么告诉她有 ...