Channel

本文介绍了Java NIO Channel的概念,并通过实例展示了如何使用RandomAccessFile实现双向读写。重点讲解了FileInputStream、FileOutputStream的通道不支持双向操作,而RandomAccessFile的特性使其能够同时支持读写操作。示例代码包括了读取标准输入、写入标准输出以及使用RandomAccessFile读写文件的场景。
摘要由CSDN通过智能技术生成

Channel

通道

双向:
	以文件流为例:
		根据输入(fileInputStream)/输出流(fileOutputStram).getChanel的通道不具备双向的特性,
		可以使用RandomAccessFile具备双向的特性,即支持读也支持写
import lombok.SneakyThrows;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;

public class TestChannel {
    @SneakyThrows
    public static void main(String[] args) {
        byte[] bytes = new byte[10];

        InputStream in = System.in;

        while (true) {
            int len;
            while ((len = in.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
        }
    }

    static class R {
        @SneakyThrows
        public static void main(String[] args) {
            ByteBuffer allocate = ByteBuffer.allocate(10);
            ReadableByteChannel readableByteChannel = Channels.newChannel(System.in);

            while (true) {
                readableByteChannel.read(allocate);
                allocate.flip();
                System.out.println(new String(allocate.array(), 0, allocate.remaining()));
                allocate.clear();
            }
        }
    }

    static class W{
        @SneakyThrows
        public static void main(String[] args) {
            WritableByteChannel writableByteChannel = Channels.newChannel(System.out);
            ByteBuffer allocate = ByteBuffer.allocate(10);

            String name = "hj";
            while (true) {
                allocate.put(name.getBytes(StandardCharsets.UTF_8));
                allocate.flip();
                writableByteChannel.write(allocate);
                allocate.clear();
            }
        }
    }

    static class RW1{
        @SneakyThrows
        public static void main(String[] args) {
            /**
             * mode:
             *  r  只读
             *  rw 读写
             *  rws 每当进行写操作,同步刷新到磁盘,刷新内容和元数据
             *  rwd 每当进行写操作,同步刷新到磁盘,刷新内容
             */
            RandomAccessFile randomAccessFile = new RandomAccessFile("test.txt", "rw");

            FileChannel channel = randomAccessFile.getChannel();
            byte[] bytes = "伞兵1号卢本伟准备就绪".getBytes(StandardCharsets.UTF_8);
            ByteBuffer allocate = ByteBuffer.allocate(bytes.length);
            allocate.put(bytes);
            allocate.flip();
            channel.write(allocate);

            //截断文件的字节数
//            channel.truncate(20);
            //赋值
//            channel.transferFrom();
//            channel.transferTo()


            System.out.println("写操作完成后文件访问位置"+channel.position());
            channel.position(0);

            ByteBuffer buf = ByteBuffer.allocate(128);
            channel.read(buf);
            buf.flip();
            System.out.println(new String(buf.array(),0, buf.remaining()));
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值