Question

I am writing a cross-platform application that creates temporary files and copies these to another location, where they need to be readable by everyone. (By default, only the owner has read access to temporary files.) I tried using the POSIX file permissions as follows:

FileAttribute<Set<PosixFilePermission>> attrs =
  PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-r--r--"));
Path temp = Files.createTempFile(null, ".tmp", attrs);

But this results in an exception on non-POSIX platforms:

java.lang.UnsupportedOperationException: 'posix:permissions' not supported as initial attribute

I want to add a simple check so that I can use the file permissions where necessary, without breaking compatibility with other platforms.

Was it helpful?

Solution

Digging deeper within JDK code, this is the check being used for POSIX

from java.nio.file.TempFileHelper

private static final boolean isPosix =
    FileSystems.getDefault().supportedFileAttributeViews().contains("posix");

You can use this check.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top