In Java, path.getFileSystem() returns the java.nio.file.FileSystem that created the path. For paths from Paths.get or Path.of that is the default file system of the computer, the same object as FileSystems.getDefault().
The FileSystem knows the rules for its paths: the separator (getSeparator()), how to make more paths (getPath), how to match names with globs (getPathMatcher), and which provider handles it, such as file for disks or jar for ZIP archives.
An instance method of the java.nio.file.Path interface, available since Java 7. Call it on a Path object, as in path.getFileSystem(...), after import java.nio.file.Path;.
Syntax
Call it on any path, then use the returned object:
Java
FileSystem fs = path.getFileSystem();
String sep = fs.getSeparator(); // "/" or "\"
PathMatcher m = fs.getPathMatcher("glob:*.txt");
Call
What it does
p.getFileSystem().getSeparator()
The name separator, / or \.
p.getFileSystem().getPath("a", "b")
Another path of the same file system.
p.getFileSystem().getPathMatcher("glob:*.txt")
A matcher for file name patterns.
Since Java 7. Never call close() on the default file system: it throws UnsupportedOperationException. File systems you open yourself, such as ZIP file systems, must be closed.
Outputs below are shown for Linux and macOS. On Windows, Java prints \ between names instead of /; the note above each output says when a result really differs.
Example 1 — Look at the File System of a Path
Print the separator, the provider scheme, and a few properties of the file system behind an ordinary path.
The default file system.Paths.get always uses the computer's default file system, so getFileSystem() returns exactly the object from FileSystems.getDefault().
The provider is "file". The provider behind ordinary disk paths uses the URI scheme file, the same scheme that toUri() writes.
Always open, writable. The default file system cannot be closed and is not read-only, unlike, for example, a ZIP archive opened read-only.
Example 2 — Build Path Text with the Right Separator
When a path must be written as one String, join the names with getSeparator(). Compare it with getPath and File.separator.
Java
import java.io.File;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathFileSystemSeparator {
public static void main(String[] args) {
FileSystem fs = Paths.get("logs").getFileSystem();
String sep = fs.getSeparator();
String joined = String.join(sep, "logs", "2026", "app.log");
Path fromText = Paths.get(joined);
Path fromParts = fs.getPath("logs", "2026", "app.log");
System.out.println("names in joined text: " + fromText.getNameCount());
System.out.println("equal to getPath: " + fromText.equals(fromParts));
System.out.println("same file system: " + (fromParts.getFileSystem() == fs));
System.out.println("same as File.separator: " + sep.equals(File.separator));
}
}
Output
Run with java PathFileSystemSeparator. The output is the same on Windows, macOS, and Linux.
names in joined text: 3
equal to getPath: true
same file system: true
same as File.separator: true
How It Works
Ask instead of guessing.getSeparator() returns the separator of the running system, so String.join builds text that parses into three names on Windows as well as on Linux.
getPath does the same job.fs.getPath("logs", "2026", "app.log") joins the names for you and gives an equal path from the same file system.
Two APIs, one answer. On the default file system the NIO separator and the old File.separator are the same String.
Example 3 — Match File Names with a Glob Pattern
Ask the file system for two PathMatchers: one for documents and one for Java files in any folder. Notice which paths the **/ pattern misses.
Braces list alternatives.*.{txt,md} means any name ending in .txt or .md, so the two documents match and photo.jpg does not.
A single star stays in one folder.* never crosses a separator, so src/main/App.java does not match the document pattern even if it had the right extension.
**/ needs at least one folder.**/*.java matches src/main/App.java, but App.java alone has no separator for the / in the pattern, so it is missed; add a second pattern such as *.java for that case.
Frequently Asked Questions
The FileSystem object that created the path. For paths from Paths.get or Path.of that is the default file system, FileSystems.getDefault().
Call path.getFileSystem().getSeparator() or FileSystems.getDefault().getSeparator(). It is / on Linux and macOS and \ on Windows.
An object from getPathMatcher that tests paths against a glob or regex pattern, such as glob:*.txt. Call matcher.matches(path).
Yes. FileSystems.newFileSystem can open a ZIP or JAR archive as a file system; its paths come from that FileSystem and cannot be mixed with normal disk paths.
Not the default one: closing it throws UnsupportedOperationException. Close only file systems you opened yourself, preferably with try-with-resources.