Java Reference
In-Depth Information
try {
FileStore fs = Files.getFileStore(path);
printDetails(fs, AclFileAttributeView.class);
printDetails(fs, BasicFileAttributeView.class);
printDetails(fs, DosFileAttributeView.class);
printDetails(fs, FileOwnerAttributeView.class);
printDetails(fs, PosixFileAttributeView.class);
printDetails(fs, UserDefinedFileAttributeView.class);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public static void printDetails(FileStore fs,
Class<? extends FileAttributeView> attribClass) {
// Check if the file attribute view is supported
boolean supported = fs.supportsFileAttributeView(attribClass);
System.out.format("%s is supported: %s%n",
attribClass.getSimpleName(), supported);
}
}
AclFileAttributeView is supported: true
BasicFileAttributeView is supported: true
DosFileAttributeView is supported: true
FileOwnerAttributeView is supported: true
PosixFileAttributeView is supported: false
UserDefinedFileAttributeView is supported: true
Reading and Updating File Attributes
The NIO.2 API provides many ways to work with file attributes. Sometimes it may be confusing to decide the method
that you want to use to manage the attributes of a file.
You may need to work with only one attribute or many attributes of a file at a time. If you need to read or update
the value of only one attribute of a file, you need to look at the available methods in the
Files
class that let you read/
update that specific attribute. For example, if you want to check if a file is a directory, use the
Files.isDirectory()
method. If you want to read the owner of a file, use the
Files.getOwner()
method. If you want to update the owner of
a file, use the
Files.setOwner()
method. The
Files
class has the following two
static
methods that let you read and
update a file attribute using the attribute name as a string:
•
Object getAttribute(Path path, String attribute, LinkOption... options)
Path setAttribute(Path path, String attribute, Object value, LinkOption...
options)
If you need to read or update multiple attributes of a file, you need to work with a specific file attribute view.
The type of attributes determines the file attribute view that you need to use. For most of the file attribute views,
you have to work with two interfaces named as
XxxAttributes
and
XxxAttributeView
. For example, for the basic
•