Java FileSystems tutorial with examples
This class defines the getDefault() method to get the default file system and factory methods to construct other types of file systems.
The first invocation of any of the methods defined by this class causes the default FileSystemProvider to be loaded.
The default provider, identified by the URI scheme «file», creates the FileSystem that provides access to the file systems accessible to the Java virtual machine.
If the process of loading or initializing the default provider fails then an unspecified error is thrown.
The first invocation of the FileSystemProvider installedProviders() method, by way of invoking any of the newFileSystem methods defined by this class, locates and loads all installed file system providers.
Installed providers are loaded using the service-provider loading facility defined by the ServiceLoader class.
Installed providers are loaded using the system class loader.
If the system class loader cannot be found then the platform class loader is used.
Providers are typically installed by placing them in a JAR file on the application class path, the JAR file contains a provider-configuration file named java.nio.file.spi.FileSystemProvider in the resource directory META-INF/services, and the file lists one or more fully-qualified names of concrete subclass of FileSystemProvider that have a zero argument constructor.
The ordering that installed providers are located is implementation specific.
If a provider is instantiated and its FileSystemProvider getScheme() returns the same URI scheme of a provider that was previously instantiated then the most recently instantiated duplicate is discarded.
URI schemes are compared without regard to case.
During construction a provider may safely access files associated with the default provider but care needs to be taken to avoid circular loading of other installed providers.
If circular loading of installed providers is detected then an unspecified error is thrown.
This class also defines factory methods that allow a ClassLoader to be specified when locating a provider.
As with installed providers, the provider classes are identified by placing the provider configuration file in the resource directory META-INF/services.
If a thread initiates the loading of the installed file system providers and another thread invokes a method that also attempts to load the providers then the method will block until the loading completes.
Example
The following code shows how to use FileSystems from java.nio.file.
import java.nio.file.FileSystems; import java.nio.file.Path; public class Main < public static void main(String[] args) < Path path = FileSystems.getDefault().getPath("/home/docs/status.txt"); > >
import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; public class Main < public static void main(String[] args) throws IOException < FileSystem fileSystem = FileSystems.getDefault(); System.out.println(fileSystem.getSeparator()); > >
import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; public class Main < public static void main(String[] args) throws IOException < FileSystem fileSystem = FileSystems.getDefault(); System.out.println(fileSystem.isOpen()); > >
Related
- Java FileSystem getRootDirectories() Returns an object to iterate over the paths of the root directories.
- Java java.nio.file FileSystems
- Java FileSystems Create from zip file
- Java FileSystems tutorial with examples
- Java FileSystems getDefault()
- Java FileSystems newFileSystem(URI uri, Map env)
- Java FileSystems newFileSystem(Path path, ClassLoader loader)
demo2s.com | Email: | Demo Source and Support. All rights reserved.
Java FileSystem tutorial with examples
Provides an interface to a file system and is the factory for objects to access files and other objects in the file system.
Introduction
Provides an interface to a file system and is the factory for objects to access files and other objects in the file system.
The default file system, obtained by invoking the FileSystems#getDefault method, provides access to the file system that is accessible to the Java virtual machine.
The FileSystems class defines methods to create file systems that provide access to other types of (custom) file systems.
- The getPath() method converts a system dependent path string, returning a *Path* object that may be used to locate and access a file.
- The getPathMatcher() method is used to create a *PathMatcher* that performs match operations on paths.
- The getFileStores() method returns an iterator over the underlying FileStore.
- The getUserPrincipalLookupService() method returns the *UserPrincipalLookupService* to lookup users or groups by name.
- The newWatchService() method creates a *WatchService* that may be used to watch objects for changes and events.
File systems vary greatly.
In some cases the file system is a single hierarchy of files with one top-level root directory.
In other cases it may have several distinct file hierarchies, each with its own top-level root directory.
The getRootDirectories() method may be used to iterate over the root directories in the file system.
A file system is typically composed of one or more underlying FileStore that provide the storage for the files.
Theses file stores can also vary in the features they support, and the file attributes or meta-data that they associate with files.
A file system is open upon creation and can be closed by invoking its close() method.
Once closed, any further attempt to access objects in the file system cause ClosedFileSystemException to be thrown.
File systems created by the default FileSystemProvider cannot be closed.
A FileSystem can provide read-only or read-write access to the file system.
Whether or not a file system provides read-only access is established when the FileSystem is created and can be tested by invoking its isReadOnly() method.
Attempts to write to file stores by means of an object associated with a read-only file system throws ReadOnlyFileSystemException.
File systems are safe for use by multiple concurrent threads.
The close() method may be invoked at any time to close a file system but whether a file system is asynchronously closeable is provider specific and therefore unspecified.
In other words, if a thread is accessing an object in a file system, and another thread invokes the close method then it may require to block until the first operation is complete.
Closing a file system causes all open channels, watch services, and other (Closeable closeable) objects associated with the file system to be closed.
Example
The following code shows how to use FileSystem from java.nio.file.
import java.io.IOException; import java.nio.file.FileStore; import java.nio.file.FileSystem; import java.nio.file.FileSystems; public class Main < public static void main(String[] args) throws IOException < FileSystem fileSystem = FileSystems.getDefault(); for (FileStore store : fileSystem.getFileStores()) < System.out.println(store.getUsableSpace()); > > >
import java.io.IOException; import java.nio.file.FileStore; import java.nio.file.FileSystem; import java.nio.file.FileSystems; public class Main < public static void main(String[] args) throws IOException < FileSystem fileSystem = FileSystems.getDefault(); for (FileStore store : fileSystem.getFileStores()) < System.out.println(store.isReadOnly()); > > >
import java.io.IOException; import java.nio.file.FileStore; import java.nio.file.FileSystem; import java.nio.file.FileSystems; public class Main < public static void main(String[] args) throws IOException < FileSystem fileSystem = FileSystems.getDefault(); for (FileStore store : fileSystem.getFileStores()) < System.out.println(store.name()); > > >
Related
- Java FileStore isReadOnly() Tells whether this file store is read-only.
- Java FileStore equals(Object obj) Indicates whether some other object is «equal to» this one.
- Java java.nio.file FileSystem
- Java FileSystem tutorial with examples
- Java FileSystem getFileStores()
- Java FileSystem getRootDirectories()
- Java FileSystem close()
demo2s.com | Email: | Demo Source and Support. All rights reserved.