FileSystem

The FileSystem class is part of the java.nio.file package.It represents a file system, which provides access to file stores, file attributes, and files themselves.

FileSystem acts as a factory for Path objects.It abstracts the underlying file system (e.g., Windows, Unix, ZIP file system).You don’t create FileSystem instances directly; instead, you obtain them via the FileSystems utility class.

How to Obtain a FileSystem Instance

Commonly Used Methods

Simple Program Using FileSystem

Problem Statement:

Write a Java program that obtains the default file system and prints all root directories on the system.

import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class SimpleFileSystemExample {
    public static void main(String[] args) {
        FileSystem fs = FileSystems.getDefault();
        
        System.out.println("Root directories in the default file system:");
        for (Path root : fs.getRootDirectories()) {
            System.out.println(root);
        }
    }
}

Problem Statement:

Create a Java program that:

  1. Uses the default FileSystem.
  2. Lists all file stores (disks/partitions) and prints their name, type, total space, and usable space.
  3. Lists all root directories.
  4. Reads and prints the supported file attribute views.
  5. Checks if the file system is read-only or open.
import java.nio.file.*;
import java.nio.file.attribute.FileStoreAttributeView;
import java.io.IOException;

public class FileSystemDetails {

    public static void main(String[] args) {
        FileSystem fs = FileSystems.getDefault();

        System.out.println("Listing file stores:");
        for (FileStore store : fs.getFileStores()) {
            try {
                System.out.println("Name: " + store.name());
                System.out.println("Type: " + store.type());
                System.out.println("Total Space (bytes): " + store.getTotalSpace());
                System.out.println("Usable Space (bytes): " + store.getUsableSpace());
                System.out.println("-------------------------");
            } catch (IOException e) {
                System.err.println("Error getting store info: " + e.getMessage());
            }
        }

        System.out.println("\nRoot directories:");
        for (Path root : fs.getRootDirectories()) {
            System.out.println(root);
        }

        System.out.println("\nSupported File Attribute Views:");
        for (String view : fs.supportedFileAttributeViews()) {
            System.out.println(view);
        }

        System.out.println("\nIs the file system read-only? " + fs.isReadOnly());
        System.out.println("Is the file system open? " + fs.isOpen());
    }
}

FileSystem abstracts the actual file system of the OS or custom file systems.

  • Use FileSystems.getDefault() to get the default system.
  • Provides methods to access roots, file stores, attribute views.
  • Useful for querying system disk info and creating/manipulating Path objects.
Scroll to Top