The file group owner can be set with the POSIX attribute named group. - Java File Path IO

Java examples for File Path IO:Unix File

Description

The file group owner can be set with the POSIX attribute named group.

Demo Code

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;

public class Main {
  public static void main(String[] args) {

    Path path = Paths.get("/home/folder1/folder2/folder3/test.txt");
    try {// w  w w  . j  a  v a2  s  .com
      GroupPrincipal group = path.getFileSystem()
          .getUserPrincipalLookupService()
          .lookupPrincipalByGroupName("apressteam");
      Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(
          group);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials