Java IO & NIO - Files.getPosixFilePermissions() Examples

Code Snippets Java IO & NIO Java 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static Set<PosixFilePermission> getPosixFilePermissions(Path path,
                                                               LinkOption... options)
                                                        throws IOException

Returns a file's POSIX file permissions.

POSIX is a family of standards specified by the IEEE for maintaining compatibility between operating systems.

Note that not all O.S. support POSIX. For example Windows does not support it by default, in that case this method will throw UnsupportedOperationException

Parameters:
path - the path to the file
options - options indicating how symbolic links are handled
Returns:
the file permissions


Examples


Running in windows will throw UnsupportedOperationException:

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;

public class GetPosixFilePermissionsExample {

public static void main(String... args) throws IOException {
System.out.println("OS: " + System.getProperty("os.name"));
Path tempFile = Files.createTempFile("test-file", ".txt");
Set<PosixFilePermission> posixFilePermissions = Files.getPosixFilePermissions(tempFile);
posixFilePermissions.stream().forEach(System.out::println);
}
}

Running in Linux Mint 18.1:

 OS: Linux
OWNER_WRITE
OWNER_READ




See Also