java set file permissions to 777 while creating a file object [duplicate]
Asked Answered
A

3

19

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?

Acorn answered 3/6, 2011 at 23:17 Comment(1)
In JDK7 you can use Files.setPosixFilePermissions to change the file permissions to whatever you want. If you need the permissions set atomically when creating the file then you can do that too.Gausman
G
17

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

Guajardo answered 3/6, 2011 at 23:21 Comment(0)
W
32

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
Ween answered 3/6, 2011 at 23:17 Comment(1)
Note that this does not work in Windows linkMilagrosmilam
C
28

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);
Chrissychrist answered 3/6, 2011 at 23:23 Comment(3)
it is not working for me..can u help?Disjunctive
Yes this does not works for files on RH LinuxMervin
If this way doesn't work for you, you can try the solution given by Stephen Ostermiller https://mcmap.net/q/627714/-java-set-file-permissions-to-777-while-creating-a-file-object-duplicateSwab
G
17

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

Guajardo answered 3/6, 2011 at 23:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.