NIO之Channel类

简介:
基本上,所有的IO在NIO中都从一个Channel开始。Channel有点像流,数据可以从Channel读到Buffer中,也可以从Buffer写到Channel中。在这里插入图片描述
Channel类虽然像流,但是和流有很大的区别,主要有以下两点:

  1. Channel只能操作Buffer中的数据,程序也不能直接访问Channel中的数据,只能通过Buffer。
  2. Channel可以将硬盘中的文件直接映射为Buffer。

JAVA中一些主要的Channel的实现

  1. FileChannel
  2. DatagramChannel
  3. SocketChannel
  4. ServerSocketChannel

FileChannel的基本使用
Java NIO中的FileChannel是一个连接到文件的通道。可以通过该通道读写文件。
FileChannel无法设置为阻塞模式,因为它总是运行在阻塞模式下。

  1. 创建FileChannel

在使用FileChannel之前,必须先创建它。创建方式有两种
第一种:使用一个InputStream、OutputStream或RandomAccessFile来获取一个FileChannel实例。

		//1 创建RandomAccessFile
         RandomAccessFile raf = new RandomAccessFile("D:\\FileText\\Hello.txt","r");
         //2 获取通道
        FileChannel channel = raf.getChannel();

第二种:在JDK1.7之后才能使用,FileChannel.open()方法。

FileChannel channel = FileChannel.open(Paths.get("D:\\FileText\\Hello.txt"), StandardOpenOption.READ);
  1. 从FileChannel读取数据
    调用read()方法可以读取FileChannel中的数据。
    首先,分配一个Buffer,将FileChannel中读取中的数据将被读到Buffer中。
    然后,调用FileChannel.read()方法。该方法将数据从FileChannel读取到Buffer中。read()方法返回的int值表示有该数据大小的字节读取到了Buffer中,如果返回值是-1,表示到了文件末尾。
 //读取
        ByteBuffer buf = ByteBuffer.allocate(1024);
        while (channel.read(buf)>0){
            //切换为读取模式
            buf.flip();
            String data = new String(buf.array(),0,buf.limit());
            System.out.println(data);
            buf.clear();
        }
  1. 向FileChannel中写数据
    使用FileChannel.write()方法向FileChannel写数据,该方法的参数是一个Buffer。
    注意因为无法保证write()方法一次能向FileChannel写完所有字节,因此需要重复调用write()方法,直到Buffer中已经没有尚未写入通道的字节。所以推荐在while循环中调用写方法。
public class ChannelWrite {
    public static void main(String[] args) throws Exception {
        FileChannel fileChannel = FileChannel.open(Paths.get("out.txt"), StandardOpenOption.WRITE);
        String data = "大哥大嫂过年好~";
        ByteBuffer buf = ByteBuffer.allocate(1024);
        buf.put(data.getBytes());
        buf.flip();
        fileChannel.write(buf);
        buf.clear();
        fileChannel.close();

    }
}

4.关闭FileChannel
用完FileChannel后需调用close()方法将其关闭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java NIO中的Channel是用于在Java程序中进行I/O操作的基本组件之一。Channel提供了一种高效的、可扩展的方式来进行数据传输,同时也支持非阻塞式I/O操作。 下面是一些常用的Channel方法: 1. `open()`:打开一个新的Channel对象。 2. `close()`:关闭当前的Channel对象。 3. `read(ByteBuffer dst)`:从Channel中读取数据到指定的ByteBuffer中。 4. `write(ByteBuffer src)`:将数据从指定的ByteBuffer写入到Channel中。 5. `configureBlocking(boolean block)`:设置当前Channel的阻塞模式,如果为true则为阻塞模式,如果为false则为非阻塞模式。 6. `register(Selector sel, int ops)`:将当前的Channel对象注册到指定的Selector中,并且指定关注的事件型。 7. `isOpen()`:判断当前的Channel对象是否处于打开状态。 8. `isConnected()`:判断当前的Channel对象是否已经连接到远程服务器。 9. `finishConnect()`:完成Channel对象的连接操作,如果连接成功则返回true,否则返回false。 10. `bind(SocketAddress local)`:将当前的Channel对象绑定到指定的本地地址。 11. `getRemoteAddress()`:获取当前Channel对象连接的远程服务器地址。 12. `getLocalAddress()`:获取当前Channel对象绑定的本地服务器地址。 这些方法提供了基本的Channel操作,可以根据具体的需求进行使用。需要注意的是,Channel对象在使用完毕后需要调用close()方法进行关闭,否则可能会出现资源泄漏等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值