jimfs:Java内存文件系统,脱离磁盘IO瓶颈利器

一、写在前面

开源地址:https://github.com/google/jimfs

Jimfs由Google发布,实现了 java.nio.file 接口,支持模拟不同操作系统的文件系统特性(如 Windows/Linux 路径风格)。
主要用于单元测试(避免磁盘 IO 影响速度和状态)、临时数据处理(短期缓存、中间计算结果)和环境隔离(多任务互不干扰)等场景。

<dependency>
    <groupId>com.google.jimfs</groupId>
    <artifactId>jimfs</artifactId>
    <version>1.3.1</version>
</dependency>

二、使用

1、基本使用

import com.google.common.collect.ImmutableList;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;

import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

public class Test {

    public static void main(String[] args) throws Exception {
        // Linux系统目录
        FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
        Path foo = fs.getPath("/foo");
        Files.createDirectory(foo);

        // 写入hello.txt
        Path hello = foo.resolve("hello.txt"); // /foo/hello.txt
        Files.write(hello, ImmutableList.of("hello world"), StandardCharsets.UTF_8);

        // 读取
        List<String> lines = Files.readAllLines(hello, StandardCharsets.UTF_8);
        for (String line : lines) {
            System.out.println(line);
        }
    }
}

2、注意

jimfs只能使用java.nio相关的API,不过这已经足够。

其他更多使用方式参考java.nio.file.Files即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秃了也弱了。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值