In-memory file system implementation for Java that provides complete java.nio.file API compatibility
—
The Jimfs class provides static factory methods for creating in-memory file systems with different configuration options.
import com.google.common.jimfs.Jimfs;
import com.google.common.jimfs.Configuration;
import java.nio.file.FileSystem;Create a file system with platform-appropriate defaults.
public static FileSystem newFileSystem();Creates a file system using Configuration.forCurrentPlatform() which selects Unix, Mac OS X, or Windows configuration based on the current operating system.
Usage Example:
FileSystem fs = Jimfs.newFileSystem();
Path root = fs.getPath("/"); // Unix-style on Linux/Mac, "C:\" style on WindowsCreate a file system with a custom name for URI identification.
public static FileSystem newFileSystem(String name);The file system uses the given name as the host part of its URI. For example, with name "test-fs", the file system URI will be jimfs://test-fs and path URIs will be jimfs://test-fs/path/to/file.
Parameters:
name - String name for the file system (used in URI host part)Usage Example:
FileSystem fs = Jimfs.newFileSystem("my-test-filesystem");
// File system URI: jimfs://my-test-filesystem
// Path URI example: jimfs://my-test-filesystem/work/data.txtCreate a file system with a specific configuration.
public static FileSystem newFileSystem(Configuration configuration);Parameters:
configuration - Configuration object defining file system behaviorUsage Example:
Configuration config = Configuration.unix()
.toBuilder()
.setMaxSize(1024 * 1024 * 1024) // 1GB limit
.setBlockSize(4096) // 4KB blocks
.setSupportedFeatures(Feature.LINKS, Feature.SYMBOLIC_LINKS)
.build();
FileSystem fs = Jimfs.newFileSystem(config);Create a file system with both a custom name and configuration.
public static FileSystem newFileSystem(String name, Configuration configuration);Parameters:
name - String name for the file systemconfiguration - Configuration object defining file system behaviorUsage Example:
Configuration winConfig = Configuration.windows()
.toBuilder()
.setWorkingDirectory("C:\\temp")
.setAttributeViews("basic", "dos")
.build();
FileSystem fs = Jimfs.newFileSystem("windows-test", winConfig);All Jimfs file systems use the URI scheme:
public static final String URI_SCHEME = "jimfs";This constant defines the scheme used in all Jimfs file system URIs.
File systems created by these methods:
java.nio.file APIsfileSystem.close()Paths.get(URI)Once created, Jimfs file systems work with all standard Java NIO.2 APIs:
FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
// Standard path operations
Path workDir = fs.getPath("/work");
Path dataFile = workDir.resolve("data.txt");
// Standard file operations
Files.createDirectories(workDir);
Files.write(dataFile, "Hello World".getBytes());
List<String> lines = Files.readAllLines(dataFile);
// Standard stream operations
try (InputStream in = Files.newInputStream(dataFile)) {
// Process file content
}File system creation methods may throw:
IllegalArgumentException - If the name parameter contains invalid characters for URI constructionAssertionError - If an unexpected IOException occurs during file system setup (this should not happen in normal operation)Install with Tessl CLI
npx tessl i tessl/maven-com-google-jimfs--jimfs