How can I set the umask from within java?

心不动则不痛 提交于 2019-12-17 07:48:11

问题


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


回答1:


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").




回答2:


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




回答3:


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.




回答4:


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())


来源:https://stackoverflow.com/questions/3175303/how-can-i-set-the-umask-from-within-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!