Java IO Tutorial

Java – Unable to assign group write permission to a file

In Java, we can use the NIO createFile() to assign file permission during file creation.

Files.java

package java.nio.file;

public static Path createFile(Path path, FileAttribute<?>... attrs)
       throws IOException

But, the createFile() fails to assign the group writes file permission to a file on the Unix system?


  Path path = Paths.get("/home/mkyong/test/runme.sh");
  Set<PosixFilePermission> perms =
        PosixFilePermissions.fromString("rwxrwxrwx");

  Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));
Terminal

$ ls -lsah

0 -rwxr-xr-x  1 mkyong mkyong    0 Jul  20 16:18 runme.sh

The Answer
In Java, assign file permission during file creation, first create the file, and assign its file permission later.


  Path path = Paths.get("/home/mkyong/test/runme.sh");

  Files.createFile(path);
  Files.setPosixFilePermissions(path,
          PosixFilePermissions.fromString("rwxrwxrwx"));

1. PosixFilePermission.GROUP_WRITE

This example tries to assign a 777 file permission during file creation.

CreateFile1.java

package com.mkyong.io.temp;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.EnumSet;
import java.util.Set;

public class CreateFile1 {

    public static void main(String[] args) {

        try {

            Path path = Paths.get("/home/mkyong/test/test1.log");

            Set<PosixFilePermission> perms = EnumSet.of(
                    PosixFilePermission.OWNER_READ,
                    PosixFilePermission.OWNER_WRITE,
                    PosixFilePermission.OWNER_EXECUTE,
                    PosixFilePermission.GROUP_READ,
                    PosixFilePermission.GROUP_WRITE,
                    PosixFilePermission.GROUP_EXECUTE,
                    PosixFilePermission.OTHERS_READ,
                    PosixFilePermission.OTHERS_WRITE,
                    PosixFilePermission.OTHERS_EXECUTE
            );

            Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output, the createFile() failed to assign the group writes file permission?

Terminal

$ ls -lsah

total 8.0K
4.0K drwxr-xr-x  2 mkyong mkyong 4.0K Jul  20 16:18 .
4.0K drwxr-xr-x 42 mkyong mkyong 4.0K Jul  20 16:06 ..

   0 -rwxr-xr-x  1 mkyong mkyong    0 Jul  20 16:18 test1.log

2. PosixFilePermissions.fromString("rwxrwxrwx")

This example uses the PosixFilePermissions.fromString to assign the 777 file permission during file creation.

CreateTempFile2.java

package com.mkyong.io.temp;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class CreateTempFile2 {

    public static void main(String[] args) {

        try {

            Path path = Paths.get("/home/mkyong/test/test2.log");

            Set<PosixFilePermission> perms =
                    PosixFilePermissions.fromString("rwxrwxrwx");

            Files.createFile(path, PosixFilePermissions.asFileAttribute(perms));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output – The result is the same; the createFile() is still unable to assign the group writes file permission?

Terminal

$ ls -lsah

total 8.0K
4.0K drwxr-xr-x  2 mkyong mkyong 4.0K Jul  20 16:22 .
4.0K drwxr-xr-x 42 mkyong mkyong 4.0K Jul  20 16:06 ..

   0 -rwxr-xr-x  1 mkyong mkyong    0 Jul  20 16:18 test1.log
   0 -rwxr-xr-x  1 mkyong mkyong    0 Jul  20 16:22 test2.log

3. Create First, Assign Later.

The file permission may be "override by the UNIX umask" (not sure) during the file creation.

To fix it or get the desire 777 file permission, we have to create a file first and assign the file permission later.

CreateTempFile3.java

package com.mkyong.io.temp;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;

public class CreateTempFile3 {

    public static void main(String[] args) {

        try {

            Path path = Paths.get("/home/mkyong/test/test3.log");

            /*Set<PosixFilePermission> perms = EnumSet.of(
                    PosixFilePermission.OWNER_READ,
                    PosixFilePermission.OWNER_WRITE,
                    PosixFilePermission.OWNER_EXECUTE,
                    PosixFilePermission.GROUP_READ,
                    PosixFilePermission.GROUP_WRITE,
                    PosixFilePermission.GROUP_EXECUTE,
                    PosixFilePermission.OTHERS_READ,
                    PosixFilePermission.OTHERS_WRITE,
                    PosixFilePermission.OTHERS_EXECUTE
            );*/

            Set<PosixFilePermission> perms =
                    PosixFilePermissions.fromString("rwxrwxrwx");

            Files.createFile(path);
            Files.setPosixFilePermissions(path, perms);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output, finally, we can assign the group writes file permission to a file.

Terminal

$ ls -lsah

total 8.0K
4.0K drwxr-xr-x  2 mkyong mkyong 4.0K Jul  20 16:27 .
4.0K drwxr-xr-x 42 mkyong mkyong 4.0K Jul  20 16:06 ..

   0 -rwxr-xr-x  1 mkyong mkyong    0 Jul  20 16:18 test1.log
   0 -rwxr-xr-x  1 mkyong mkyong    0 Jul  20 16:22 test2.log

   0 -rwxrwxrwx  1 mkyong mkyong    0 Jul  20 16:27 test3.log

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments