FANDOM


Introduction Edit

This page introduced more advanced usages of the API.

Advanced metadata usage Edit

In order to access more detailed data, the API provides different FileAttributeViews. All of them have a name.

An attribute view supports both read and write access to file metadata. Note that for some of the views defined below, you will need to further call a method to get a read-only view of the defined attributes (while this is not defined by an interface, all such views do this using a method named readAttributes()).

The standard JDK defines the following views:

All of these views give read, and sometimes write, access to the attributes they allow access to. These attributes also have a name, and the javadoc links above mention these names.

You can get the list of supported attribute views by name with the following code for a given Path:

path.getFileSystem().supportedFileAttributeViews()

This returns a Set<String>.

Unfortunately, there is no method to get a FileAttributeView by name... You have to use the class to access it. This is done using Files.getFileAttributeView(). For instance:

// Get the "posix" attribute view on a Unix system; do not follow symlinks
final PosixFileAttributeView view = Files.getFileAttributeView(thePath, PosixFileAttributeView.class,
    LinkOption.NOFOLLOW_LINKS);

Another possibility is to query, or set, attributes by name; this is done using respectively Files.getAttribute() and Files.setAttribute(). For instance, here is how you could obtain the list of ACLs (if the provider supports ACLs):

// You need to cast, since all methods querying/setting by name only return/accept Objects as arguments.
// The javadoc for each view gives you the real types:
@SuppressWarnings("unchecked")
final List<AclEntry> acls 
    = (List<AclEntry>) Files.getAttribute(thePath, "acl:acl");

You can also get a map of attributes for a given view; this is done using Files.readAttributes().

In the example below we read all basic attributes at once; note however that the values of the returned map are Objects, so you will need to refer to the relevant attribute view documentation to know what the types of the values are:

final Map<String, Object> attrs = Files.readAttributes(path, "basic:*");

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.