java在创建文件对象时将文件权限设置为777

发布于 2024-11-13 11:52:13 字数 235 浏览 3 评论 0原文

可能的重复:
如何在 java 中设置 umask?

在java中创建文件对象时,如何将文件权限设置为777(或任何其他任意权限)?

Possible Duplicate:
How can I set the umask from within java?

How do you set file permissions to 777(or any other arbitrary permission) while creating a file object in java?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风透绣罗衣 2024-11-20 11:52:13

Java SE 7 有 java.nio.file .attribute.PosixFileAttributes 为您提供对所有者、组和其他人的读、写和执行权限的细粒度控制。

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Set;

public class Test {
    public static void main(String[] args) throws Exception {
        Path path = Paths.get("/tmp/test-file.txt");
        if (!Files.exists(path)) Files.createFile(path);
        Set<PosixFilePermission> perms = Files.readAttributes(path,PosixFileAttributes.class).permissions();

        System.out.format("Permissions before: %s%n",  PosixFilePermissions.toString(perms));

        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        perms.add(PosixFilePermission.GROUP_WRITE);
        perms.add(PosixFilePermission.GROUP_READ);
        perms.add(PosixFilePermission.GROUP_EXECUTE);
        perms.add(PosixFilePermission.OTHERS_WRITE);
        perms.add(PosixFilePermission.OTHERS_READ);
        perms.add(PosixFilePermission.OTHERS_EXECUTE);
        Files.setPosixFilePermissions(path, perms);

        System.out.format("Permissions after:  %s%n",  PosixFilePermissions.toString(perms));
    }
}

然后可以像这样使用:

$ rm -f /tmp/test-file.txt && javac Test.java && java Test
Permissions before: rw-r--r--
Permissions after:  rwxrwxrwx

Java SE 7 has java.nio.file.attribute.PosixFileAttributes which gives you fine grained control over read, write, and execute permissions for owner, group, and others.

import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Set;

public class Test {
    public static void main(String[] args) throws Exception {
        Path path = Paths.get("/tmp/test-file.txt");
        if (!Files.exists(path)) Files.createFile(path);
        Set<PosixFilePermission> perms = Files.readAttributes(path,PosixFileAttributes.class).permissions();

        System.out.format("Permissions before: %s%n",  PosixFilePermissions.toString(perms));

        perms.add(PosixFilePermission.OWNER_WRITE);
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_EXECUTE);
        perms.add(PosixFilePermission.GROUP_WRITE);
        perms.add(PosixFilePermission.GROUP_READ);
        perms.add(PosixFilePermission.GROUP_EXECUTE);
        perms.add(PosixFilePermission.OTHERS_WRITE);
        perms.add(PosixFilePermission.OTHERS_READ);
        perms.add(PosixFilePermission.OTHERS_EXECUTE);
        Files.setPosixFilePermissions(path, perms);

        System.out.format("Permissions after:  %s%n",  PosixFilePermissions.toString(perms));
    }
}

Which can then be used like:

$ rm -f /tmp/test-file.txt && javac Test.java && java Test
Permissions before: rw-r--r--
Permissions after:  rwxrwxrwx
谜兔 2024-11-20 11:52:13

有 3 种方法可用:

setReadalble(布尔布尔值)
setWritable(boolean,布尔值)
setExecutable(boolean, boolean)

这会将文件设置为“0777”

String path = "SOME/PATH";

final File file = new File(path);
file.setReadable(true, false);
file.setExecutable(true, false);
file.setWritable(true, false);

3 methods are available:

setReadalble(boolean boolean)
setWritable(boolean,boolean)
setExecutable(boolean,boolean)

This will set the file to "0777"

String path = "SOME/PATH";

final File file = new File(path);
file.setReadable(true, false);
file.setExecutable(true, false);
file.setWritable(true, false);
峩卟喜欢 2024-11-20 11:52:13

如果在启动 JVM 之前将 umask(2) 设置为 0,则创建的所有文件和目录都将具有所有人的完全权限。这可能是一个坏主意。

您可以使用 File.setReadable()File.setWritable创建文件后修改模式位的 API。如果您授予权限,这通常就足够了;如果您尝试删除其他用户的权限,那么您的权限可能应该从一开始就设置得非常严格。 (umask(0777) 在启动 JVM 之前,然后在您想要的位置添加权限。)

If you set the umask(2) to 0 before starting the JVM, all files and directories created will be created with full permissions for everyone. This is probably a bad idea.

You can use the File.setReadable(), File.setWritable APIs to fiddle with the mode bits after the file has been created. That's often good enough, if you're granting permissions; if you're trying to remove permissions from other users, then your permissions should probably be set very restrictively from the start. (umask(0777) before launching the JVM, then add permissions exactly where you want them.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文