NIO读写文件,简单Util直接拿去用

NIO读写文件,简单Util直接拿去用

java.nio全称java non-blocking IO,是指jdk1.4 及以上版本里提供的新api(New IO) ,为所有的原始类型(boolean类型除外)提供缓存支持的数据容器,使用它可以提供非阻塞式的高伸缩性网络。

Java IO和NIO之间第一个最大的区别是,IO是面向流的,NIO是面向缓冲区的。

GitHub: link. 欢迎star

@Slf4j
public class NIOUtil {

    /**
     * 读文件返回String
     *
     * @param path 文件路径
     * @return 读取内容
     */
    public static String readFile(String path) {
        String string = null;
        RandomAccessFile randomAccessFile = null;
        try {
            randomAccessFile = new RandomAccessFile(path, "rw");
            FileChannel channel = randomAccessFile.getChannel();

            ByteBuffer allocate = ByteBuffer.allocate(1024 << 4); //16KB缓冲区
            List<Byte> byteList = new ArrayList<>();
            byte[] array;
            int length;
            while ((length = channel.read(allocate)) != -1) {
                array = allocate.array();
                for (int i = 0; i < length; i++) {
                    byteList.add(array[i]);
                }
                allocate.clear();
            }
            channel.close();
            byte[] arr = new byte[byteList.size()];
            for (int i = 0; i < byteList.size(); i++) {
                arr[i] = byteList.get(i);
            }
            string = new String(arr, StandardCharsets.UTF_8);
        } catch (Exception e) {
            log.error("readFile failure!! error={} message={}", e, e.getMessage());
            e.printStackTrace();
        } finally {
            if (null != randomAccessFile) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    log.error("randomAccessFile.close() failure!! error={} message={}", e, e.getMessage());
                }
            }
        }
        return string;
    }

    /**
     * String写文件
     *
     * @param text 内容
     * @param path 目标文件路径
     */
    public static void writeFile(String text, String path) {
        RandomAccessFile randomAccessFile = null;
        try {
            Files.deleteIfExists(Paths.get(path)); //目标文件存在先删除
            randomAccessFile = new RandomAccessFile(path, "rw");
            FileChannel channel = randomAccessFile.getChannel();
            channel.write(ByteBuffer.wrap(text.getBytes(StandardCharsets.UTF_8)));
            channel.close();
        } catch (Exception e) {
            log.error("writeFile failure!! error={} message={}", e, e.getMessage());
            e.printStackTrace();
        } finally {
            if (null != randomAccessFile) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    log.error("randomAccessFile.close() failure!! error={} message={}", e, e.getMessage());
                }
            }
        }
    }
}

GitHub: link. 欢迎star

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值