java.nio.file.FileSystem class in java
Last Updated :
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:
Constructor | Description |
---|
FileSystem() | Creates a new object of FileSystem class |
Methods of this class are as follows:
Method | Description |
---|
close() | Closes this file system that has been opened. |
getFileStores() | Returns an iterable object, which is used to iterate over the underlying file stores. |
getPath(String first, String... more) | Converts given string to a Path or combines a given sequence of strings to form a Path. |
getPathMatcher(String syntaxAndPattern) | Returns a PathMatcher object which is used to perform match operations on the Path objects. |
getRootDirectories() | Returns an iterable object, which is used to iterate over the root directories. |
getSeparator() | Returns string representation of the name separator. |
getUserPrincipalLookupService() | Returns the UserPrincipalLookupService object for this file system. It is an optional operation. |
isOpen() | Used to check whether this file system is open or not. |
isReadOnly() | Used to check whether this file system allows read-only access to its file stores or not. |
newWatchService() | Returns a new WatchService object. |
provider() | Returns the provider that created this file system. |
supportedFileAttributeViews() | Returns the set consisting of the names of the file attribute views supported by this file system. |
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 classes
import java.io.*;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
// Main class
public 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 classes
import java.io.*;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
// Main class
public 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