开发者

How can I set the umask from within java?

开发者_JAVA技巧

I'm new to Java. Where is umask exposed in the api?


You can't fiddle with the umask directly, since Java is an abstraction and the umask is POSIX-implementation specific. But you have the following API:

File f;
f.setExecutable(true);
f.setReadable(false);
f.setWritable(true);

There are some more APIs available, check the docs.

If you must have direct access to the umask, either do it via JNI and the chmod() syscall, or spawn a new process with exec("chmod").


import java.nio.file.Files
import java.nio.file.attribute.PosixFilePermission

File file = new File("/some/path") 
Files.setPosixFilePermissions(file.toPath(), [
                PosixFilePermission.OWNER_READ,
                PosixFilePermission.OWNER_WRITE
            ].toSet())


java.nio.file.attribute.PosixFileAttributes in Java SE 7.


Another approach is to use a 3rd-party Java library that exposes POSIX system calls; e.g.

  • Jtux
  • The "Posix for Java" library,
  • and so on (Google for "java posix library").

The problem with this approach is that it is intrinsically non-portable (won't work on a non-POSIX compliant platform), and requires a platform-specific native library ... and all that that entails.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜