零基础java自学流程-Java语言高级536

559 篇文章 0 订阅
546 篇文章 0 订阅

通道(Channel)

在NIO程序中服务器端和客户端之间的数据读写不是通过流,而是通过通道来读写的。

通道类似于流,都是用来读写数据的,但它们之间也是有区别的:

  • 通道是双向的,即可以读也可以写,而流是单向的,只能读或写

  • 通道可以实现异步读写数据

  • 通道可以从缓冲区读数据,也可以把数据写入缓冲区

java中channel的相关类在java.nio.channel包下。Channel是一个接口,其常用的实现类有:

  • FileChannel:用于文件的数据读写,其真正的实现类为FileChannelImpl

  • DatagramChannel:用于UDP的数据读写,其真正的实现类为DatagramChannelImpl

  • ServerSocketChannel:用于监听TCP连接,每当有客户端连接时都会创建一个SocketChannel,功能类似ServerSocket,其真正的实现类为ServerSocketChannelImpl

  • SocketChannel:用于TCP的数据读写,功能类似节点流+Socket,其真正的实现类为SocketChannelImpl

FileChannel

FileChannel主要用于对本地文件进行IO操作,如文件复制等。它的常用方法有:

在文件传输流中有个属性channel,它默认是空的,可以通过流中的getChanel()方法根据当前文件流的属性生成对应的FileChannel。

public FileChannel getChannel() {
        synchronized (this) {
            if (channel == null) {
                channel = FileChannelImpl.open(fd, path, false, true, append, this);
            }
            return channel;
        }
    }
}

下面是通道使用的代码实例

public class NIOChannel {
    public static void main(String[] args) throws IOException {
    }

    //将数据写入目标文件
    public static void writeFile() throws IOException{
        String str = "Hello, gofy";
        //创建文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream("f:\\file.txt");
        //根据文件输出流生成文件通道
        FileChannel fileChannel = fileOutputStream.getChannel();
        //创建字节缓冲区,并将字符串转成字节存入
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        byteBuffer.put(str.getBytes());
        //注意,在存入后需要进行写出操作时,需将缓冲区翻转
        byteBuffer.flip();
        //将缓冲区数据写入通道
        fileChannel.write(byteBuffer);
        //将文件输出流关闭(该方法同时会关闭通道)
        fileOutputStream.close();
    }

    //从文件中读取数据
    public static void readFile() throws IOException{
        //创建文件输入流
        File file = new File("f:\\file.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //根据文件输入流生成文件通道
        FileChannel fileChannel = fileInputStream.getChannel();
        //创建字节缓冲区,大小为文件大小
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
        //将通道数据读入缓冲区
        fileChannel.read(byteBuffer);
        //同样,在读入后需要取出缓冲区内所有数据时,需将缓冲区翻转
        byteBuffer.flip();
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }

    //将文件数据传输到另一个文件
    public static void readAndWriteFile() throws IOException{
        //创建文件输入流和文件输出流,并生成对应的通道
        FileInputStream fileInputStream = new FileInputStream("file1.txt");
        FileChannel inputStreamChannel= fileInputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("file2.txt");
        FileChannel outputStreamChannel = fileOutputStream.getChannel();
        //创建字节缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //进行数据读取
        while (true){
            //在读取前需清除缓冲区
            byteBuffer.clear();
            //将文件输入的通道的数据读入缓冲区
            int read = inputStreamChannel.read(byteBuffer);
            //当read为-1时,即通道数据已读取完毕
            if (read == -1){
                break;
            }
            //将缓冲区翻转后,将缓冲区数据写入文件输出的通道
            byteBuffer.flip();
            outputStreamChannel.write(byteBuffer);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    //文件的复制粘贴
    public static void copyAndPaste() throws IOException{
        //复制的文件输入流
        FileInputStream fileInputStream = new FileInputStream("f:\\a.jpg");
        FileChannel srcChannel = fileInputStream.getChannel();
        //粘贴的文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream("f:\\b.jpg");
        FileChannel targetChannel = fileOutputStream.getChannel();
        //使用transferFrom进行复制粘贴
        targetChannel.transferFrom(srcChannel, 0, srcChannel.size());
        fileInputStream.close();
        fileOutputStream.close();
    }
}

尚学堂给同学们带来全新的Java300集课程啦!java零基础小白自学Java必备优质教程_手把手图解学习Java,让学习成为一种享受_哔哩哔哩_bilibili尚学堂给同学们带来全新的Java300集课程啦本课程为Java300集2022版第一季,配合最新版的Java课程,所有视频重新录制,课件所有图形做了重新绘制和配色,图解学习Java,让学习成为一种享受本套教程专门为零基础学员而制,适合准备入行Java开发的零基础学员,视频中穿插多个实战项目。每一个知识点都讲解的通俗易懂,由浅入深。不仅适用于零基础的初学者,有经验的程序员也可做巩固学习。后续课https://www.bilibili.com/video/BV1qL411u7eE?spm_id_from=333.999.0.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值