通道表示打开到 IO 设备(例如:文件、套接字)的连接。若需要使用 NIO 系统,需要获取用于连接 IO 设备的通道以及用于容纳数据的缓冲区。然后操作缓冲区,对数据进行处理。Channel 负责传输, Buffer 负责存储。通道是由 java.nio.channels 包定义的。 Channel 表示 IO 源与目标打开的连接。Channel 类似于传统的“流”。只不过 Channel本身不能直接访问数据, Channel 只能与Buffer 进行交互。

java.nio.channels.Channel 接口:

|--FileChannel  本地文件的通道

  |--SocketChannel

          |--ServerSocketChannel

          |--DatagramChannel

网络的关于通道

获取通道

1. Java 针对支持通道的类提供了 getChannel() 方法

本地 IO:

FileInputStream/FileOutputStream

RandomAccessFile

网络IO:

Socket

ServerSocket

DatagramSocket

2. 在 JDK 1.7 中的 NIO.2 针对各个通道提供了静态方法 open()

3. 在 JDK 1.7 中的 NIO.2 的 Files 工具类的 newByteChannel()

package com.toov5.Nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.junit.jupiter.api.Test; public class BUfferTest02 {
// 非直接缓冲区 读写操作
@Test
public void BUfferTest() throws IOException {
// 读入流
FileInputStream fileInputStream = new FileInputStream("aa.jpg");
// 写入流
FileOutputStream fileOutputStream = new FileOutputStream("bb.jpg");
// 创建通道
FileChannel iChannel = fileInputStream.getChannel(); // 读入流的通道
FileChannel oChannel = fileOutputStream.getChannel(); // 写入流的通道
//分配制定缓冲区大小
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (iChannel.read(byteBuffer) != -1) {
//开启读取模式
byteBuffer.flip();
//将数据写入到通道中
oChannel.write(byteBuffer);
byteBuffer.clear();
}
//关闭通道、关闭连接
iChannel.close();
oChannel.close();
fileInputStream.close();
fileOutputStream.close(); } }

运行结果:

直接缓冲区:

package com.toov5.Nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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.jupiter.api.Test; public class BUfferTest02 {
@Test
public void testZhiJie() throws IOException {
//创建管道
FileChannel inChannel =FileChannel.open(Paths.get("aa.jpg"), StandardOpenOption.READ);
FileChannel outChannel =FileChannel.open(Paths.get("bb.jpg"),StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE );
//定义映射文件
MappedByteBuffer inMappedByteBuffer =inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
MappedByteBuffer outMappedByteBuffer =outChannel.map(MapMode.READ_WRITE, 0, inChannel.size());
//直接对缓冲区操作
byte[] bytes = new byte[inMappedByteBuffer.limit()];
inMappedByteBuffer.get(bytes);
outMappedByteBuffer.put(bytes);
inChannel.close();
outChannel.close();
System.out.println("操作直接缓冲区完毕");
} // 非直接缓冲区 读写操作
@Test
public void BUfferTest() throws IOException {
// 读入流
FileInputStream fileInputStream = new FileInputStream("aa.jpg");
// 写入流
FileOutputStream fileOutputStream = new FileOutputStream("bb.jpg");
// 创建通道
FileChannel iChannel = fileInputStream.getChannel(); // 读入流的通道
FileChannel oChannel = fileOutputStream.getChannel(); // 写入流的通道
//分配制定缓冲区大小
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (iChannel.read(byteBuffer) != -1) {
//开启读取模式
byteBuffer.flip();
//将数据写入到通道中
oChannel.write(byteBuffer);
byteBuffer.clear();
}
//关闭通道、关闭连接
iChannel.close();
oChannel.close();
fileInputStream.close();
fileOutputStream.close(); } }

通道(Channel)的原理获取的更多相关文章

  1. NIO之通道(Channel)的原理与获取以及数据传输与内存映射文件

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

  2. Java-NIO(四):通道(Channel)的原理与获取

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

  3. 通道(Channel)的原理与获取

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

  4. Java NIO中的通道Channel(一)通道基础

    什么是通道Channel 这个说实话挺难定义的,有点抽象,不过我们可以根据它的用途来理解: 通道主要用于传输数据,从缓冲区的一侧传到另一侧的实体(如文件.套接字...),反之亦然: 通道是访问IO服务 ...

  5. go中的数据结构通道-channel

    1. channel的使用 很多文章介绍channel的时候都和并发揉在一起,这里我想把它当做一种数据结构来单独介绍它的实现原理. channel,通道.golang中用于数据传递的一种数据结构.是g ...

  6. 详解 通道 (Channel 接口)

    在本篇博文中,本人主要讲解NIO 的两个核心点 -- 缓冲区(Buffer) 和 通道 (Channel)之一的 缓冲区(Buffer), 有关NIO流的其他知识点请观看本人博文<详解 NIO流 ...

  7. nio再学习之通道channel

    通道(Channel):用于在数据传输过程中,进行输入输出的通道,其与(流)Stream不一样,流是单向的,在BIO中我们分为输入流,输出流,但是在通道中其又具有读的功能也具有写的功能或者两者同时进行 ...

  8. 理解CNN中的通道 channel

    在深度学习的算法学习中,都会提到 channels 这个概念.在一般的深度学习框架的 conv2d 中,如 tensorflow .mxnet ,channels 都是必填的一个参数. channel ...

  9. 33、[源码]-AOP原理-获取拦截器链-MethodInterceptor

    33.[源码]-AOP原理-获取拦截器链-MethodInterceptor

随机推荐

  1. DFRobot万物互联大赛第一轮

    前言 原创文章,转载引用务必注明链接,水平有限,如有疏漏,欢迎指正. DF搞的这个比赛还挺有趣:micro:bit × OBLOQ DF创客社区玩转物联网挑战赛,一边在写文章一边在爱奇艺上看着印度电影 ...

  2. 微信小程序 - 考试状态不同显示

    未开考 .已交卷. 考试中 .考试结束 #ddd      #f00     #ff0    默认禁用色 禁用的button仅有style起作用,四个状态,通过wx:if ... elif ... e ...

  3. java程序如何优化--技巧总结

    http://www.douban.com/group/topic/17850695/

  4. 团队项目的Git分支管理规范

    原文地址: http://blog.jboost.cn/2019/06/17/git-branch.html 许多公司的开发团队都采用Git来做代码版本控制.如何有效地协同开发人员之间,以及开发.测试 ...

  5. request 防盗链

    package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...

  6. hdu 1068 Girls and Boys 二分图的最大匹配

    题目链接:pid=1068">http://acm.hdu.edu.cn/showproblem.php? pid=1068 #include <iostream> #in ...

  7. JavaScript--基于对象的脚本语言学习笔记(一)

    1.两种嵌入js的方式    使用javascript前缀构建url:<a href="javascript:alert('执行JavaScript. .')">执行j ...

  8. 关于海康视频采集卡的简介---基于pci的插潮采集卡

    vga 640x480 qvga vga的1/4,宽高分别是vga的一半 (1)采集类型 海康威视 DS-2CE16A2P-IT3P 700TVL 1/3" DIS ICR 红外防水筒型摄像 ...

  9. WCF服务返回XML或JSON格式数据

    第一种方式public string GetData( string format) { string res = null; Student stu = new Student { StuID = ...

  10. 资源:Localization – 本地化

    Resource Dictionary –资源字典 所有的资源项在最终都会被整合到Resource Dictionary中的,也就是说无论是FrameworkElement的Resources,还是W ...