To access the method of a filesystem we need to:
- Obtain a reference to a java.nio.file.FileSystem object.
- Use the methods of this object to access filesystem information.
How to do it...
1- Create a new console application. Add the following code to the main method of the application.
This sequence displays several fileSystem attributes, including the filesystem provider, file open status, whether the file is available to be read-only, the root directories, and the names of the file stores:
2. Execute the application. Your output will depend upon the configuration of your system. However, it should mimic the output that follows:
How it works...
The getDefault method returned the default filesystem used by the JVM. Next, several methods were invoked against this object:
- The provider method returned the provider, that is, implementer of the filesystem.
In this case, it was a Windows filesystem provider that came bundled with the JVM.
- The isOpen method indicated that the filesystem is open and ready for use.
- The isReadOnly method returned false, meaning that we can read and write to the system.
- We used the getRootDirectories method to create an Iterable object that permitted us to list each root directory.
- The getFileStores method returned another Iterable object, which was used to display the names of the file stores.
While we do not normally need to close a filesystem, the close method can be used to close the filesystem. Any subsequent methods executed against the filesystem will result in a ClosedFileSystemException being thrown. Any open channels, directory streams, and watch services associated with the filesystem will also be closed. Note that the default filesystem cannot be closed.
The FileSystems class' getFileSystem method can be used to access a specific filesystem. In addition, the overloaded newFileSystem method will create new filesystems. The close method can be used with these instances.
Filesystems are thread-safe. However, if one thread attempts to close the filesystem while another thread is accessing the filesystem object, the close operation may be blocked until the access is complete.
- Java 7 New Features ebook
- The power of java 7 NIO.2 (JSR 203) (important concepts