java NIO读取文件

一、什么是NIO?

java 传统的IO是阻塞的,也叫做BIO,在读写操作完成之前,线程一直会处于阻塞状态。
而NIO则不同,他是非阻塞IO,NIO在进行读写操作时,采用管道流的方式读写,同事不断的去调用操作系统,检查操作系统是否读写完成。只有操作系统完成后才可以使用数据,这样避免了线程处于阻塞状态

二、NIO组件介绍

Channel、Buffere、Selector是NIO的核心API,IO在NIO中从一个Channel,数据可以从Channel读取到Buffer中,也可以从Buffer写入到Chaanel
(1)Channel常用的类型有一下几种:FileChannel、DatagramChannel、SocketChannel、ServerSocketChannel,这些通道分别是文件IO,UDP和TCP IO
(2)Buffer常用的有IntBuffer、FloatBuffer、CharBuffer、DoubleBuffer、ShortBuffer、LongBuffer、ByteBuffer
(3)Selectot提供了单线程处理多个Channel的能力,首先需要将Channel注册到Selector中

三、NIO的使用

1、使用FileChannel

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
public class FileChannelDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            fis = new FileInputStream("C:\\Users\\42515\\Desktop\\test\\file1.txt");
            File outFile = new File("C:\\Users\\42515\\Desktop\\test\\file2.txt");
            if (!outFile.exists()) {
                outFile.createNewFile();
            }
            fos = new FileOutputStream(outFile, true);
            inChannel = fis.getChannel();
            outChannel = fos.getChannel();

            //高效拷贝文件,避免了两次用户态和内核态的上下文切换
            inChannel.transferTo(0, inChannel.size(), outChannel);

            ByteBuffer br = ByteBuffer.allocate(48);
            while (inChannel.read(br) != -1) {
                //设置限制位置为当前位置position,也就是读写指针的位置,限制位置limit指的是在该模式下最多能读写多少数据,然后将position设置为0,表示将存缓存头部开始读;
                //调用flip之后,读写指针指到缓存头部,并且设置了最多只能读出之前写入的数据长度(而不是整个缓存的容量大小)
                br.flip();
                //写入数据
                outChannel.write(br);
                //将position设置为0,limit为缓冲区的大小,也就是初始化指针的位置,实际上并没有清除缓冲区的数据
                br.clear();
                //此处可以继续读取缓冲区的数据
                System.out.println(new String(br.array(), StandardCharsets.UTF_8));

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inChannel != null) {
                    inChannel.close();
                }
                if (outChannel != null) {
                    outChannel.close();
                }
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
···


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 Java NIO 读取文件需要以下步骤: 1. 创建一个文件通道(FileChannel)对象,通过 FileInputStream 的 getChannel() 方法获取。 2. 创建一个字节缓冲区(ByteBuffer)对象,用于存放从文件读取的数据。 3. 调用通道的 read() 方法,将数据从文件读取到缓冲区中。 4. 循环读取数据,直到文件末尾。 5. 关闭文件通道和文件输入流。 下面是一个示例代码,演示如何使用 Java NIO 读取文件: ```java import java.io.FileInputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class NIOFileReader { public static void main(String[] args) throws Exception { FileInputStream inputStream = new FileInputStream("filename.txt"); FileChannel channel = inputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (channel.read(buffer) != -1) { buffer.flip(); // 切换读模式 while (buffer.hasRemaining()) { System.out.print((char) buffer.get()); // 读取数据 } buffer.clear(); // 切换写模式 } channel.close(); inputStream.close(); } } ``` 在上面的代码中,我们首先创建了一个文件输入流(FileInputStream)对象,并通过 getChannel() 方法获取了一个文件通道(FileChannel)对象。然后我们创建了一个字节缓冲区(ByteBuffer)对象,并指定了缓冲区的大小为 1024 字节。接下来,我们使用 while 循环读取数据,每次读取一部分数据到缓冲区中,然后切换读模式,读取数据,再切换写模式,清空缓冲区。最后,我们关闭了文件通道和文件输入流。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值