Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建文件时,如何设置其他人的写入?_Java_Clojure_Java.nio.file - Fatal编程技术网

Java 创建文件时,如何设置其他人的写入?

Java 创建文件时,如何设置其他人的写入?,java,clojure,java.nio.file,Java,Clojure,Java.nio.file,我正试图与派生自“rw-rw-rw-”的FileAttribute一起使用。我的文件正在Fedora上创建为“rw-rw-r--” 创建文件时,如何设置其他人的写入 例如: (->> "rw-rw-rw-" PosixFilePermissions/fromString PosixFilePermissions/asFileAttribute vector into-array (Files/createFile (Paths/get

我正试图与派生自
“rw-rw-rw-”
的FileAttribute一起使用。我的文件正在Fedora上创建为
“rw-rw-r--”

创建文件时,如何设置其他人的写入

例如:

(->> "rw-rw-rw-"
     PosixFilePermissions/fromString
     PosixFilePermissions/asFileAttribute
     vector
     into-array
     (Files/createFile (Paths/get "temp" (into-array String []))))
;;; temp is created as rw-rw-r--

在类unix系统中,每个进程都有一个名为的属性,该属性被屏蔽到由子进程创建和继承的任何文件的权限上。默认值为
0002
或“关闭为其他人写入”。因此,Java很可能正在设置您所寻求的权限,然后将其屏蔽。您可以在启动java进程之前显式设置创建文件的权限或更改java进程的umask设置

首先让我们看看当前的umask:

arthur@a:/tmp$ umask
0002
然后,让我们为每个人制作一个具有读写功能的文件(touch和您的java代码一样可以实现这一功能)

我们看到八进制
0002
已从实际文件权限中屏蔽出来。
因此,我们可以通过将此umask设置为0000来删除它:

arthur@a:/tmp$ umask 0000
arthur@a:/tmp$ touch foo
我们看到,由于umask只适用于新文件,所以foo在更新时保持原样。并使用“读取其他”权限创建一个新的文件栏

arthur@a:/tmp$ ls -l foo
-rw-rw-r-- 1 arthur arthur 0 Aug 28 14:00 foo
arthur@a:/tmp$ touch bar
arthur@a:/tmp$ ls -l bar
-rw-rw-rw- 1 arthur arthur 0 Aug 28 14:00 bar
我习惯于在用Java创建文件后显式设置权限,因为这样更容易从一个系统传送到另一个系统。您可以在运行emacs/程序之前在shell中设置umask,然后在运行emacs/程序之后检查文件权限,从而证明这一点

*Java是“一次编写,在任何地方运行”吗

arthur@a:/tmp$ ls -l foo
-rw-rw-r-- 1 arthur arthur 0 Aug 28 14:00 foo
arthur@a:/tmp$ touch bar
arthur@a:/tmp$ ls -l bar
-rw-rw-rw- 1 arthur arthur 0 Aug 28 14:00 bar