In-memory file system implementation for Java that provides complete java.nio.file API compatibility
npx @tessl/cli install tessl/maven-com-google-jimfs--jimfs@1.3.0Jimfs is an in-memory file system implementation for Java 8+ that provides complete compatibility with the java.nio.file abstract file system APIs. It enables developers to create virtual file systems in memory for testing, sandboxing, and temporary file operations without touching the actual filesystem.
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<version>1.3.0</version>
</dependency>import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
// Create a new in-memory file system with Unix-style configuration
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
// Create directories and files
Path workDir = fs.getPath("/work");
Files.createDirectory(workDir);
Path configFile = workDir.resolve("config.txt");
Files.write(configFile, Arrays.asList("key=value", "debug=true"), StandardCharsets.UTF_8);
// Read and manipulate files
List<String> lines = Files.readAllLines(configFile, StandardCharsets.UTF_8);
System.out.println("Config loaded: " + lines.size() + " lines");Jimfs is built around several key components:
Jimfs class provides static factory methods for creating configured file systemsConfiguration objects define file system behavior, path types, and feature supportFactory methods for creating in-memory file systems with platform-specific or custom configurations.
public static FileSystem newFileSystem();
public static FileSystem newFileSystem(String name);
public static FileSystem newFileSystem(Configuration configuration);
public static FileSystem newFileSystem(String name, Configuration configuration);Comprehensive configuration system for customizing file system behavior, from path handling to storage limits.
public static Configuration unix();
public static Configuration osX();
public static Configuration windows();
public static Configuration forCurrentPlatform();
public static Builder builder(PathType pathType);
public Builder toBuilder();Path type system supporting different operating system conventions with Unicode normalization options.
public static PathType unix();
public static PathType windows();
public enum PathNormalization {
NONE, NFC, NFD, CASE_FOLD_UNICODE, CASE_FOLD_ASCII
}Optional feature system and configurable directory watching capabilities.
public enum Feature {
LINKS, SYMBOLIC_LINKS, SECURE_DIRECTORY_STREAM, FILE_CHANNEL
}
public static WatchServiceConfiguration polling(long interval, TimeUnit timeUnit);Jimfs file systems return standard FileSystem instances that work seamlessly with all java.nio.file APIs:
Files.createDirectory(), Files.createFile(), Files.delete()Files.copy(), Files.move(), Files.walk()Files.newInputStream(), Files.newOutputStream()Files.newByteChannel(), FileChannel.open()Files.newDirectoryStream(), WatchService supportConfiguration objects are immutable and thread-safeFileSystem instances support concurrent access from multiple threadsConfiguration.Builder objects are not thread-safe and should not be shared between threads