In-memory file system implementation for Java that provides complete java.nio.file API compatibility
—
Jimfs supports different path types that define how paths are parsed, rendered, and handled, along with Unicode and case normalization options.
import com.google.common.jimfs.PathType;
import com.google.common.jimfs.PathNormalization;
import java.util.regex.Pattern;Creates a Unix-style path type with / separators.
public static PathType unix();Characteristics:
///\0) in pathsUsage Example:
PathType pathType = PathType.unix();
Configuration config = Configuration.builder(pathType)
.setRoots("/")
.setWorkingDirectory("/home/user")
.build();Creates a Windows-style path type supporting both \ and / separators.
public static PathType windows();Characteristics:
\/ when parsing pathsC:\)\\host\share\)C:foo)\foo)Usage Example:
PathType pathType = PathType.windows();
Configuration config = Configuration.builder(pathType)
.setRoots("C:\\", "D:\\")
.setWorkingDirectory("C:\\Users\\user")
.build();The PathNormalization enum provides options for normalizing path names to handle Unicode and case sensitivity.
public enum PathNormalization implements Function<String, String> {
NONE, NFC, NFD, CASE_FOLD_UNICODE, CASE_FOLD_ASCII
}NONEApplies no normalization to path names. Paths are used exactly as provided.
NFCApplies Unicode Normalization Form Composed (NFC). Combines character sequences into their composed forms.
Usage Example:
Configuration config = Configuration.unix()
.toBuilder()
.setNameDisplayNormalization(PathNormalization.NFC)
.build();NFDApplies Unicode Normalization Form Decomposed (NFD). Breaks down composed characters into their component parts.
Usage Example:
// Mac OS X typically uses NFD for canonical form
Configuration config = Configuration.unix()
.toBuilder()
.setNameCanonicalNormalization(PathNormalization.NFD)
.build();CASE_FOLD_UNICODEApplies full Unicode case folding for case-insensitive paths. Requires ICU4J library on the classpath.
Usage Example:
Configuration config = Configuration.unix()
.toBuilder()
.setNameCanonicalNormalization(PathNormalization.CASE_FOLD_UNICODE)
.build();Error Handling:
Throws NoClassDefFoundError if ICU4J is not available:
PathNormalization.CASE_FOLD_UNICODE requires ICU4J.
Did you forget to include it on your classpath?CASE_FOLD_ASCIIApplies ASCII-only case folding for simple case-insensitive paths. Converts ASCII characters to lowercase.
Usage Example:
Configuration config = Configuration.windows()
.toBuilder()
.setNameCanonicalNormalization(PathNormalization.CASE_FOLD_ASCII)
.build();Apply a single normalization to a string.
public abstract String apply(String string);Each normalization enum value implements this method to transform the input string.
Usage Example:
String normalized = PathNormalization.CASE_FOLD_ASCII.apply("Hello World");
// Result: "hello world"Get regex pattern flags that approximate the normalization.
public int patternFlags();Returns flags suitable for use with Pattern.compile() to create regex patterns that match the normalization behavior.
Usage Example:
int flags = PathNormalization.CASE_FOLD_ASCII.patternFlags();
// Returns: Pattern.CASE_INSENSITIVEApply multiple normalizations in sequence.
public static String normalize(String string, Iterable<PathNormalization> normalizations);Parameters:
string - Input string to normalizenormalizations - Sequence of normalizations to applyUsage Example:
String result = PathNormalization.normalize("Héllo Wörld",
Arrays.asList(PathNormalization.NFD, PathNormalization.CASE_FOLD_ASCII));Create a regex pattern using flags from multiple normalizations.
public static Pattern compilePattern(String regex, Iterable<PathNormalization> normalizations);Parameters:
regex - Regular expression stringnormalizations - Normalizations to derive pattern flags fromUsage Example:
Pattern pattern = PathNormalization.compilePattern(".*\\.txt",
Arrays.asList(PathNormalization.CASE_FOLD_ASCII));
// Creates case-insensitive pattern for .txt filesJimfs distinguishes between two forms of path names:
Path.toString() and path renderingConfiguration config = Configuration.unix()
.toBuilder()
.setNameDisplayNormalization(PathNormalization.NFC) // For display
.setNameCanonicalNormalization(PathNormalization.NFD, PathNormalization.CASE_FOLD_ASCII) // For lookup
.setPathEqualityUsesCanonicalForm(true) // Use canonical form for Path.equals()
.build();When configuring normalizations:
Configuration.osX() // Uses NFC for display, NFD + CASE_FOLD_ASCII for canonicalConfiguration.windows() // Uses CASE_FOLD_ASCII for canonical formConfiguration.unix() // No normalization by default (case-sensitive)Install with Tessl CLI
npx tessl i tessl/maven-com-google-jimfs--jimfs