通道表示打开到 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. 浅谈PHP与手机APP开发即API接口开发

    API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞生很多专门API服务的公司,如:聚合数据(http ...

  2. 面试题:使用finalkeyword修饰一个变量时,是引用不能变,还是引用的对象不能变?

    /* * 问题:使用finalkeyword修饰一个变量时,是引用不能变,还是引用的对象不能变 * 答: * 使用finalkeyword修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内 ...

  3. linux 源代码安装mysql5.5

    linux下源代码安装mysql过程例如以下: yum update yum upgrade yum install -y vim man wget yum install -y gcc gcc-c+ ...

  4. scrapy之Logging使用

    #coding:utf-8 __author__ = 'similarface' ###################### ##Logging的使用 ###################### ...

  5. (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试

    Android单元和instrumentation单元測试 Developing Android unit and instrumentation tests Android的单元測试是基于JUnit ...

  6. linux history 命令 禁用history

    保存在.bash_history文件中,默认1000条,你也可以更改这个 值 !!:上一个指令 !number 运行第几个指令 查看命令历史的时间戳,那么可以执行: # export HISTTIME ...

  7. java List复制:浅拷贝与深拷贝

    Java的拷贝可以分为三种:浅拷贝(Shallow Copy).深拷贝(Deep Copy).延迟拷贝(Lazy Copy). 在java中除了基本数据类型之外(int,long,short等),还存 ...

  8. 用户对变量或寄存器进行位操作 、“|=”和“&=~”操作

    给定一个整型变量a,写两段代码,第一个设置a的bit 3,第二个清除a的bit 3.在以上两个操作中,要保持其他位不变. 答案: ----------------------------------- ...

  9. go module

    前言 go 1.5 引进了vendor管理工程依赖包,但是vendor的存放路径是在GOPATH底下,另外每个依赖还可以有自己的vendor,通常会弄得很乱,尽管dep管理工具可以将vendor平级化 ...

  10. python 基础 2.4 while 循环

    #/usr/bin/python #coding=utf-8 #@Time :2017/10/18 15:31 #@Auther :liuzhenchuan #@File :while 循环.py 示 ...