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
import java.io.*;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
public class GFG {
public static void main(String[] args)
{
try {
FileSystem filesystem
= FileSystems.getDefault();
System.out.println(
"File System created successfully" );
System.out.println(
"Underlying file stores of this FileSystem :" );
for (FileStore store :
filesystem.getFileStores()) {
System.out.println(store.toString());
}
System.out.println(
"Root directories of this FileSystem :" );
for (Path rootdir :
filesystem.getRootDirectories()) {
System.out.println(rootdir.toString());
}
}
catch (Exception e) {
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
import java.io.*;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
public class GFG {
public static void main(String[] args)
{
try {
FileSystem filesystem
= FileSystems.getDefault();
System.out.println(
"FileSystem created successfully" );
if (filesystem.isOpen())
System.out.println( "File system is open" );
else
System.out.println( "File system is close" );
if (filesystem.isReadOnly())
System.out.println(
"File system is Read-only" );
else
System.out.println(
"File system is not Read-only" );
System.out.println( "Separator: "
+ filesystem.getSeparator());
System.out.println( "Hashcode: "
+ filesystem.hashCode());
System.out.println(
"Provider: "
+ filesystem.provider().toString());
}
catch (Exception e) {
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
Share your thoughts in the comments
Please Login to comment...