Java FileSystems.newFileSystem() Method Example

1. Introduction

This blog post discusses the FileSystems.newFileSystem() method in Java. This method is part of the NIO (New I/O) package, which provides extensive support for file I/O and file system operations.


The FileSystems.newFileSystem() method is used to create a new file system that is different from the default file system. This method is particularly useful when dealing with compressed files like ZIP or JAR, as it allows you to treat them as a file system.

2. Program Steps

1. Create a path to a ZIP file.

2. Use FileSystems.newFileSystem() to create a file system for the ZIP file.

3. Read files from the ZIP file using the newly created file system.

3. Code Program

import java.nio.file.*;

public class NewFileSystemExample {
    public static void main(String[] args) {
        Path zipPath = Paths.get("");

        // Creating a new FileSystem for the ZIP file
        try (FileSystem zipFs = FileSystems.newFileSystem(zipPath, null)) {
            Path fileInZip = zipFs.getPath("/document.txt");

            // Reading a file from the ZIP file system
            System.out.println("Content of document.txt:");
        } catch (IOException e) {


Content of document.txt:
[Contents of document.txt inside]


1. A Path object, zipPath, is created for a ZIP file named

2. FileSystems.newFileSystem() creates a new file system for the ZIP file. This allows treating the ZIP file as a file system.

3. A file inside the ZIP file, /document.txt, is accessed using the new file system.

4. Files.lines() reads and prints the content of document.txt.

5. The output shows the content of document.txt from the ZIP file, demonstrating how FileSystems.newFileSystem() enables reading from compressed files as if they were regular directories.