java.nio.file.FileSystem class in java

30 Jul, 2021
java.nio.file.FileSystem class provides an interface to a file system. The file system acts as a factory for creating different objects like Path, PathMatcher, UserPrincipalLookupService, and WatchService. This object help to access the files and other objects in the file system.
Syntax: Class declaration
public abstract class FileSystem
extends Object
implements Closeable
The constructor of this class is as follows:
Methods of this class are as follows:
Example 1:
Java
// Java program to Create a new FileSystem object and// print its file stores and root directories // Importing required classes from java.nio package// Importing input output classesimport java.io.*;import java.nio.file.FileStore;import java.nio.file.FileSystem;import java.nio.file.FileSystems;import java.nio.file.Path; // Main classpublic class GFG {     // Main driver method    public static void main(String[] args)    {        // Try block to check for exceptions        try {            // Create a new File system Object            FileSystem filesystem                = FileSystems.getDefault();             // Display messages only            System.out.println(                "File System created successfully");             System.out.println(                "Underlying file stores of this FileSystem :");             // Print the Underlying file stores of this            // FileSystem using for each loop            for (FileStore store :                 filesystem.getFileStores()) {                System.out.println(store.toString());            }             // Display message only            System.out.println(                "Root directories of this FileSystem :");             for (Path rootdir :                 filesystem.getRootDirectories()) {                 // Print the Root directories of this                // FileSystem using standard toString()                // method                System.out.println(rootdir.toString());            }        }         // Catch block to handle exceptions        catch (Exception e) {             // Print the exception along with            // line number where it occurred            e.printStackTrace();        }    }}
  
Output: 
File System created successfully
Underlying file stores of this FileSystem :
/ (/dev/disk1s1)
/dev (devfs)
/private/var/vm (/dev/disk1s4)
/net (map -hosts)
/home (map auto_home)
Root directories of this FileSystem :
/
Example 2:
Java
// Java program to illustrate Working of FileSystem Class// Via its Methods // importing required classes from java.nio package// Importing input output classesimport java.io.*;import java.nio.file.FileSystem;import java.nio.file.FileSystems; // Main classpublic class GFG {     // Main driver method    public static void main(String[] args)    {        // Try block to check for exceptions        try {             // Creating an object of FileSystem class in the            // main() method using getDefault() method            FileSystem filesystem                = FileSystems.getDefault();             // Display message only            System.out.println(                "FileSystem created successfully");             // Checking if file system is open or close            if (filesystem.isOpen())                 // Print statement                System.out.println("File system is open");            else                 // Print statement                System.out.println("File system is close");             // Check if file system is read-only            if (filesystem.isReadOnly())                 // Print statement                System.out.println(                    "File system is Read-only");            else                 // Print statement                System.out.println(                    "File system is not Read-only");             // Now, print the name separator            // using getSeparator() method            System.out.println("Separator: "                               + filesystem.getSeparator());             // Print hash value of this file system            // using hashCode() method            System.out.println("Hashcode: "                               + filesystem.hashCode());             // Print provider of this file system            System.out.println(                "Provider: "                + filesystem.provider().toString());        }         // Catch block to handle the exceptions        catch (Exception e) {             // Print the exception along with line number            // using printStackTrace() method and            // display it on the console            e.printStackTrace();        }    }}
Output: 
FileSystem created successfully
File system is open
File system is not Read-only
Separator: /
Hashcode: 929338653
Provider: sun.nio.fs.MacOSXFileSystemProvider@4b1210ee 
surinderdawra388
simranarora5sos
arorakashish0911
Java-NIO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java