JavaSE 7,8: Determining Views Supported by a Particular File System

If you have questions about a file or a directory, such as whether it is hidden, whether it is a directory, what its size is, and who owns it, you can get answers to those questions (and many others) from the metadata, which is data about other data.

NIO.2 associates the notion of metadata with attributes and provides access to them through the java.nio.file.attribute package. Since different file systems have different notions about which attributes should be tracked, NIO.2 groups the attributes into views, each of which maps to a particular file system implementation.

Generally, views provide the attributes in bulk through a common method, readAttributes(). In addition, you can extract and set a single attribute with the getAttribute() and setAttribute( methods, respectively, which are available in the java.nio.file.Files class. Depending On the view, other methods are available for additional tasks.

Here i will explain how with NIO.2 you can manage more details about files’ metadata than ever before. Attributes are divided into categories, and now they cover POSIX systems as well.

Before you attempt to access a view’s attributes, make sure that your file system supports the corresponding view.

NIO.2 comes with a set of six views (BasicDos,POSIXFileOwner,ACL and UserDefinedFileAttributeView).

  • POSIX (Portable Operating System Interface for UNIX).
  • All file systems support the basic view, so you should get at least the basic name in your output.

The supported views are:

  1. BasicFileAttributeView
    This is a view of basic attributes that must be supported by all file system implementations. The attribute view name is basic.
  2. DosFileAttributeView
    This view provides the standard four supported attributes on file systems that support the DOS attributes. The attribute view name is dos.
  3. PosixFileAttributeView
    This view extends the basic attribute view with attributes supported on file systems that support the POSIX (Portable Operating System Interface for Unix) family of standards, such as Unix. The attribute view name is posix.
  4. FileOwnerAttributeView
    This view is supported by any file system implementation that supports the concept of a file owner. The attribute view name is owner.
  5. AclFileAttributeView
    This view supports reading or updating a file’s ACL. The NFSv4 ACL model is supported. The attribute view name is acl.
  6. UserDefinedFileAttributeView
    This view enables support of metadata that is user defined.

Here are code snipets that shows different operations

  • Get all supported file system views
    import static java.lang.System.out;
     
    .......
     
    FileSystem fs = FileSystems.getDefault();
     
    for (String sfsView : fs.supportedFileAttributeViews()) {
            out.println(sfsView);
            }
  • Check if the file store support a specific view
    You can test a particular view on a file store by calling the FileStore.supportsFileAttributeView() method. You can pass the desired view as a String or as a class name.
    import static java.lang.System.out;
     
    .......
     
    FileSystem fs = FileSystems.getDefault();
     
    for (FileStore fileStore : fs.getFileStores()) {
         boolean supported = fileStore.supportsFileAttributeView(BasicFileAttributeView.class);
         out.println("Is " + fileStore.name() + ": supports \'basic file attribute view\' ---> " + (supported ? "Yes" : "No"));
    }
  • Check if a file store in which a particular file resides supports a single view
    Moreover, you can check if a file store in which a particular file resides supports a single view.
    import static java.lang.System.out;
    import static java.lang.System.err;
     
    .......
     
    FileSystem fs = FileSystems.getDefault();
     
    try {
        FileStore store = getFileStore(path);
        boolean supported = store.supportsFileAttributeView("acl");
        out.println("Is " + store.name() + ": supports \'ACL file attribute view\' ---> " + (supported ? "Yes" : "No"));
    } catch (IOException ex) {
        err.println(ex);
    }

That’s all, have fun, if you like it share it.

Resources:

 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× 4 = sixteen



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close