Java 为组创建具有写权限的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/564154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:16:42  来源:igfitidea点击:

Create directory with write permissions for the group

javalinux

提问by

I create a folder in my java program (running on linux) with mkdirs() function of File object. The problem is that the folder gets only read permissions for the group. I need it to have also write permissions (in order to be able to delete its files later). What is the way to do it?

我使用 File 对象的 mkdirs() 函数在我的 java 程序(在 linux 上运行)中创建了一个文件夹。问题是该文件夹仅获得该组的读取权限。我需要它也有写权限(以便以后能够删除它的文件)。有什么方法可以做到?

回答by Rob Lachlan

On java 6, there are methods that allow you to do this, like setwriteable(). On previous versions of java, you'll have to access the command line to do a chmod command.

在 java 6 上,有一些方法可以让你做到这一点,比如 setwriteable()。在以前版本的 java 上,您必须访问命令行才能执行 chmod 命令。

Java 6 SE File Class Doc.

Java 6 SE 文件类文档。

EDIT: Woops, I'm completely wrong; I failed to notice that you wanted group permissions specifically. Those don't appear to be settable without Runtime.exec().

编辑:糟糕,我完全错了;我没有注意到您特别想要组权限。如果没有 Runtime.exec(),这些似乎是不可设置的。

@David: You're right.

@大卫:你说得对。

Another thought: if you have a lot of files to change, how about writing a shell script and calling that from runtime.exec()?

另一个想法:如果您有很多文件要更改,如何编写一个 shell 脚本并从 runtime.exec() 调用它?

回答by David Z

I suspect (unless/until someone posts an answer to the contrary) that there's no way to do this in Java's standard library, because POSIX group permissions (the rwxrwxrwx kind you're used to) are not cross-platform. Java 6 will let you set owner permissions or global permissions, but (as far as I can tell) not group permissions. If you really must do it, try using Runtime.exec("chmod g+w directory"), but it might be a good idea stylistically to wrap it in a method like setGroupWritable().

我怀疑(除非/直到有人发布相反的答案)在 Java 的标准库中无法做到这一点,因为 POSIX 组权限(您习惯的 rwxrwxrwx 类型)不是跨平台的。Java 6 将允许您设置所有者权限或全局权限,但(据我所知)不是组权限。如果您确实必须这样做,请尝试使用Runtime.exec("chmod g+w directory"),但在风格上将其包装在setGroupWritable().

回答by sateesh

OK this is not a java solution and definitely not portable.

好的,这不是 java 解决方案,绝对不可移植。

Since you mention that you are linux, probably you can think of checking the "umask" settings and setting it appropriately (to have directories created with group write permissions) and then launching your java program.

既然您提到您是 linux,那么您可能可以考虑检查“umask”设置并对其进行适当设置(使用组写入权限创建目录),然后启动您的 java 程序。

回答by Sylvain

Java nio can help with posix file attributes: https://docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFileAttributeView.html

Java nio 可以帮助处理 posix 文件属性:https: //docs.oracle.com/javase/8/docs/api/java/nio/file/attribute/PosixFileAttributeView.html

Path path = ...
 Set<PosixFilePermission> perms =
     EnumSet.of(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_WRITE);
 Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));

回答by user1679437

A very simple solution for this is:

一个非常简单的解决方案是:

file.setExecutable(true, false);
file.setReadable(true, false);
file.setWritable(true, false);

In above code, file is a File object.

在上面的代码中,file 是一个 File 对象。

When you are creating a file set these permissions to a file setExecutable(true)will allow you to set that file as Executablefor owner only. If you add one more parameter which I have added in above code file.setExecutable(true, false);will make Executablefalsefor owner only, that means it will set permissions to all group / world.

创建文件时,将这些权限设置为文件setExecutable(true)将允许您将该文件设置Executable为仅所有者。如果你加我在上面的代码中添加了一个参数file.setExecutable(true, false);将使Executablefalse业主只,这意味着它会设置权限,所有组/世界。

Tested and working on Debian and Windows XP.

在 Debian 和 Windows XP 上测试和工作。