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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top