Introduction Edit

File attributes are metadata; they are, for instance, its size, its last modification time, its owner, its permissions etc. The java.nio.file separates these attributes in views.

All filesystems are required to implement the basic file attributes. This package wraps such views in attribute providers.

You need to provide two things:

  • an implementation for the basic file attributes;
  • an attribute factory.

This package also allows you to create your own attribute views if you so desire, and this will also be explained here.

Creating an attribute provider for the basic file attributes Edit

In order to implement these, you need to:

  • decide of a class representing the metadata for your filesystem objects (file, directory or otherwise);
  • provide an implementation of BasicFileAttributesProvider; this implementation should be public, and it must have a constructor (also public) accepting an instance of your metadata class as an argument.

Note that the metadata class needs not be provided by yourself; if for instance you implement a filesystem over a content provider which provides a Java SDK, you will likely have a class suiting your needs already.

For instance, if the class is named MyMetaData:

public class MyBasicFileAttributesProvider
    extends BasicFileAttributesProvider
    // instance variables...

    // Constructor
    public MyBasicFileAttributesProvider(final MyMetaData metadata)
        throws IOException
        // constructor body

    // Implement methods

Note: some attribute providers inherit others; for instance, PosixFileAttributesProvider extends BasicFileAttributesProvider. The code will automatically account for this, so if you provide a POSIX attribute provider implementation, you need not provide a base attribute provider implementation.

Attribute factory Edit

Once you have your attribute provider class(es), you need to provide an implementation of FileAttributeFactory. In the constructor, you will (in this order!!) tell it what your metadata class is, and register your attribute provider(s). Example:

public class MyFileAttributeFactory
    extends FileAttributeFactory
    public FileAttributeFactory()
        // Set the metadata class...
        // Now register your attribute providers
        addImplmentation("basic", MyBasicFileAttributesProvider.class);
        // etc

The first argument to addImplementation is the name of the attribute view. The standard attribute views defined by the JDK are the following:

Advanced usage: creating your own attribute views Edit

If you want to create your own attribute views, you can also do this!

The process is not as simple as it is for already defined attribute views...

First of all, you first need to choose a name for the attribute view; of course, this name should not be that of a view already defined by the JDK (see above). You then need to know what attributes you provide with this view, and the name for each of them as well.

Once you have decided on a name, you need to create several things:

Once this is done, you need to provide an attribute provider implementation; it must be a subclass of FileAttributesProvider and it must implement your interface(s) above.

Then you need to register all of this to the attributes factory; first, you need to register the descriptor (which contains the name of your new view), and only then the implementation:

// 1. view interface
public interface MyFileAttributeView 
    extends FileAttributeView 
    // methods to read/write your attributes
// 2. simplify the process; no dedicated attributes class
// 3. the descriptor
public final class MyAttributesDescriptor
    // implementation
// 4. in the factory:
public final class MyFileAttributesFactory
    extends FileAttributesFactory
    public MyFileAttributesFactory()
        addDescriptor(new MyAttributesDescriptor());
        addImplementation("myview", MyAttributesProviderImpl.class);

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.