java.nio.file.FileSystems Class in Java
java.nio.file.FileSystems class acts as a factory for creating new file systems. This class provides methods for creating file systems. This file system act as factories for creating different objects like Path, PathMatcher, UserPrincipalLookupService, and WatchService. This object helps to access the files and other objects in the file system.
Syntax: Class declaration
public final class FileSystems extends Object
Methods of this class is as follows:
Method |
Description |
getDefault() |
This method returns a new default FileSystem. |
getFileSystem(URI uri) |
This method returns a reference to an existing FileSystem. |
newFileSystem(Path path, ClassLoader loader) |
This method is used to construct a new FileSystem to access the contents of the files of this file system. |
newFileSystem(URI uri, Map<String,?> env) |
This method is used to create a new File system using the given URI |
newFileSystem(URI uri, Map<String,?> env, ClassLoader loader) |
This method is used to create a new File system using the given URI |
Example 1:
Java
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:
Example 2:
Java
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class GFG {
public static void main(String[] args)
{
try {
Map<String, String> env = new HashMap<>();
Path zipPath = Paths.get( "ZipFile.zip" );
URI Uri
= new URI( "jar:file" ,
zipPath.toUri().getPath(), null );
FileSystem filesystem
= FileSystems.newFileSystem(Uri, env);
System.out.println(
"FileSystem created successfully" );
if (filesystem.isOpen())
System.out.println( "File system is open" );
else
System.out.println( "File system is close" );
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
File System created successfully
File system is open
Last Updated :
21 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...