Java Reference
In-Depth Information
if (posixView == null) {
System.out.format("POSIX attribute view is not supported%n.");
return;
}
readPermissions(posixView);
updatePermissions(posixView);
}
public static void readPermissions(PosixFileAttributeView posixView) {
try {
PosixFileAttributes attribs; attribs = posixView.readAttributes();
Set<PosixFilePermission> permissions = attribs.permissions();
// Convert the set of posix file permissions into rwxrwxrwx form
String rwxFormPermissions = PosixFilePermissions.toString(permissions);
System.out.println(rwxFormPermissions);
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public static void updatePermissions(PosixFileAttributeView posixView) {
try {
Set<PosixFilePermission> permissions =
EnumSet.of(OWNER_READ, OWNER_WRITE, GROUP_READ);
posixView.setPermissions(permissions);
System.out.println("Permissions set successfully.");
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
rw-r--r--
Permissions set successfully.
Watching a Directory for Modifications
NIO.2 supports a watch service to notify a Java program when an object in a file system is modified. Currently, you
can watch only directories for modifications. The watch service uses the native file event notification facility of the file
system. If a file system does not provide a file event notification facility, it may use other mechanisms such as polling.
The following classes and interfaces in the java.nio.file package are involved in the implementation of a
watch service:
The
Watchable interface
The
WatchService interface
The
WatchKey interface
 
Search WWH ::




Custom Search