BIO、NIO、AIO按行读取文件汇总

三种读取方式,都能按行读取文件,没有最好的,只有最适合的。根据公司的业务场景,选择适合自己的。下面贴代码:

DbConfig.java

package com.wangyh.bean;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.locks.LockSupport;

public class DbConfig {

    private static List<String> configList = new CopyOnWriteArrayList<>();

    static {
        loadConfig();
    }


    static void loadConfig() {
        try {
            String filePath = "E:\\idea_work\\com.wangyh.demo\\src\\main\\resources\\user.properties";
            readLineByNio(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * bio按行读取
     *
     * @param filePath
     */
    private static void readLineByBio(String filePath) {
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File(filePath)));
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * nio按行读取
     *
     * @param filePath
     * @throws IOException
     */
    private static void readLineByNio(String filePath) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(filePath, "rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);
        int hasReadBytes = channel.read(buffer);
        ByteBuffer stringBuffer = ByteBuffer.allocate(1024);
        while (hasReadBytes != -1) {
            System.out.println("读取字节数:" + hasReadBytes);
            buffer.flip();// 切换模式,写->读;之前是写buffer,现在要读buffer;真正的理解也不是切换模式,而是将开始位置设置为0,最大位置设置为当前位置
            boolean merge = false;
            while (buffer.hasRemaining()) {
                byte b = buffer.get();
                if (b == 10 || b == 13) { // 换行或回车,windows系统2个字符都会出现
                    merge = true;
                } else {
                    if (merge) {
                        stringBuffer.flip();
                        String line = Charset.forName("utf-8").decode(stringBuffer).toString();
                        stringBuffer.clear();//重置开始位置、最大容量
                        configList.add(line);
                        merge = false;
                    }
                    if (!stringBuffer.hasRemaining()) {
                        stringBuffer = reAllocate(stringBuffer);
                    }
                    stringBuffer.put(b);
                }
            }
            buffer.clear();// 清空,position位置为0,limit=申请的最大容量,不再是上次的读到的位置
            hasReadBytes = channel.read(buffer);//  继续往buffer中写
        }
        System.out.println(stringBuffer.position());
        if (stringBuffer.position() != 0) {//将剩下的认为是一行
            stringBuffer.flip();
            String line = Charset.forName("utf-8").decode(stringBuffer).toString();
            stringBuffer.clear();
            configList.add(line);
        }
        randomAccessFile.close();
    }

    /**
     * aio按行读取,缺陷是首次申请的bufferSize必须超过文件总大小;适用于小文件按行处理
     * 或许aio就不太适用这种业务场景,而是适合哪种不需要按行读的;只读字节类型的数据
     *
     * @param filePath
     * @throws Exception
     */
    private static void readLineByAio(String filePath) throws Exception {
        completionHandlerDemo(filePath);
        //futureDemo(asynchronousFileChannel, byteBuffer);
    }

    private static void completionHandlerDemo(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 1024);
        long position = 0;
        CountDownLatch countDownLatch = new CountDownLatch(1);
        ByteBuffer stringBuffer = ByteBuffer.allocate(1024);
        asynchronousFileChannel.read(byteBuffer, position, byteBuffer, new CompletionHandlerImpl(stringBuffer, configList, countDownLatch));
        try {
            countDownLatch.await();
            asynchronousFileChannel.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    private static void futureDemo(String filePath) throws InterruptedException, ExecutionException, IOException {
        Path path = Paths.get(filePath);
        AsynchronousFileChannel asynchronousFileChannel = AsynchronousFileChannel.open(path, StandardOpenOption.READ);
        ByteBuffer byteBuffer = ByteBuffer.allocate(16);
        Future<Integer> result = asynchronousFileChannel.read(byteBuffer, 0);
        while (!result.isDone()) {
            System.out.println("Waiting file channel finished....");
            LockSupport.parkNanos(1);
        }
        System.out.println("Finished? = " + result.isDone());
        System.out.println("byteBuffer = " + result.get());
        System.out.println(byteBuffer);
        asynchronousFileChannel.close();
    }

    private static ByteBuffer reAllocate(ByteBuffer stringBuffer) {
        final int capacity = stringBuffer.capacity();
        byte[] newBuffer = new byte[capacity * 2];
        System.arraycopy(stringBuffer.array(), 0, newBuffer, 0, capacity);
        return (ByteBuffer) ByteBuffer.wrap(newBuffer).position(capacity);
    }

    public static List<String> getConfigList() {
        return configList;
    }

}
CompletionHandlerImpl.java
package com.wangyh.bean;

import java.nio.ByteBuffer;
import java.nio.channels.CompletionHandler;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.CountDownLatch;

public class CompletionHandlerImpl implements CompletionHandler<Integer, ByteBuffer> {

    private ByteBuffer stringBuffer;

    private List<String> configList;

    private  CountDownLatch countDownLatch ;

    public CompletionHandlerImpl(ByteBuffer stringBuffer, List<String> configList, CountDownLatch countDownLatch) {
        this.stringBuffer = stringBuffer;
        this.configList = configList;
        this.countDownLatch = countDownLatch;
    }


    @Override
    public void completed(Integer readBytes, ByteBuffer buffer) {
        System.out.println("Bytes Read = " + readBytes);
        System.out.println("thread-id="+Thread.currentThread().getId());
        buffer.flip();
        boolean merge = false;
        while (buffer.hasRemaining()) {
            byte b = buffer.get();
            if (b == 10 || b == 13) { // 换行或回车,windows系统2个字符都会出现
                merge = true;
            } else {
                if (merge) {
                    stringBuffer.flip();
                    String line = Charset.forName("utf-8").decode(stringBuffer).toString();
                    stringBuffer.clear();//重置开始位置、最大容量
                    configList.add(line);
                    System.out.println(line);
                    merge = false;
                }
                if (!stringBuffer.hasRemaining()) {
                    stringBuffer = reAllocate(stringBuffer);
                }
                stringBuffer.put(b);
            }
        }
        if (stringBuffer.position() != 0) {//将剩下的认为是一行
            stringBuffer.flip();
            String line = Charset.forName("utf-8").decode(stringBuffer).toString();
            stringBuffer.clear();
            System.out.println(line);
            configList.add(line);
        }
        countDownLatch.countDown();
    }

    public void failed(Throwable exc, ByteBuffer attachment) {
        System.out.println(exc.getCause());
        countDownLatch.countDown();
    }

    private static ByteBuffer reAllocate(ByteBuffer stringBuffer) {
        final int capacity = stringBuffer.capacity();
        byte[] newBuffer = new byte[capacity * 2];
        System.arraycopy(stringBuffer.array(), 0, newBuffer, 0, capacity);
        return (ByteBuffer) ByteBuffer.wrap(newBuffer).position(capacity);
    }

}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三月泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值