重温java知识(四十、网络编程之五:NIO之三:Channel之)

通道(Channel)可以用来读取和写入数据,通道类似于之前的输入/输出流,但是程序不会直接操作通道,所有的内容都是先读取或者写入缓冲区中,再通过缓冲区取得或者写入。

通道与传统的流操作不同,传统的流操作分为输入流和输出流,,而通道本身是双向操作的,既可以完成输入也可以完成输出。

Channel本身是一个接口标准。

1、使用文件通道(FileChannel)读取数据的例子【使用该类,可以依靠FileInputStream或FileOutStream类中的getChannel()方法取得输入或输出的通道。】:

package com.mydemo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOChannelDemo {

    public static void main(String[] args) throws Exception {

        // 定义文件路径
        File file = new File("K:" + File.separator + "test.txt");

        // 文件输入流
        FileInputStream fileInputStream = new FileInputStream(file);

        // 获取文件通道
        FileChannel fileChannel = fileInputStream.getChannel();

        // 开辟缓冲大小
        ByteBuffer byteBuffer= ByteBuffer.allocate(20);

        // 内存输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        // 保存读取个数
        int count = 0;

        // 缓冲区读取
        while ((count = fileChannel.read(byteBuffer)) != -1){
            // 重置缓冲区
            byteBuffer.flip();

            // 是否还有数据
            while (byteBuffer.hasRemaining()){
                // 内容写入内存流
                byteArrayOutputStream.write(byteBuffer.get());
            }

            // 清空缓冲区
            byteBuffer.clear();
        }

        // 字节数组转字符串
        System.out.println(new String(byteArrayOutputStream.toByteArray()));

        // 关闭通道
        fileChannel.close();

        // 关闭输入流
        fileInputStream.close();
    }

}

Channel针对线程管道的I/O操作也提供专门的通道,为此定义了两个类型:

  • Pipe.SinkChannel(管道数据输出)
  • Pipe.SourceChannel(管道数据输入)

2、管道流(Pipe)的例子:

package com.mydemo;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Pipe;

public class NIOChannelDemo {

    public static void main(String[] args) throws Exception {

       // 打开管道流
        Pipe pipe = Pipe.open();

        new Thread(()->{
            // 打开管道输入流
            Pipe.SourceChannel sourceChannel = pipe.source();

            // 开辟缓存空间
            ByteBuffer byteBuffer = ByteBuffer.allocate(50);


            try {
                // 返回读取个数
                int count = sourceChannel.read(byteBuffer);
                // 重置缓存
                byteBuffer.flip();
                System.out.println("【接收端】" + new String(byteBuffer.array(), 0, count));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }, "接收线程").start();


        new Thread(()->{
            // 信息
            String msg = "【" + Thread.currentThread().getName() + "】test";

            // 获取管道输出流
            Pipe.SinkChannel sinkChannel = pipe.sink();

            // 开辟缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(50);

            // 设置要发送的数据
            byteBuffer.put(msg.getBytes());

            // 重置缓冲区
            byteBuffer.flip();

            // 缓冲区有数据
            while (byteBuffer.hasRemaining()){
                try {
                    // 输出
                    sinkChannel.write(byteBuffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }, "发送线程").start();
    }

}

运行结果:
【接收端】【发送线程】test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值