Find out the group by calling the Files.getAttribute() method - Java File Path IO

Java examples for File Path IO:File Attribute

Description

Find out the group by calling the Files.getAttribute() method

Demo Code

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

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

    Path path = Paths.get("/home/folder1/folder2/folder3/test.txt");
    try {/*w  ww.  j  av  a 2s . c om*/
      GroupPrincipal group = (GroupPrincipal) Files.getAttribute(path,
          "posix:group", LinkOption.NOFOLLOW_LINKS);
      System.out.println(group.getName());
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

POSIX attributes can be required with the following names:

  • group
  • permissions

The generally accepted form is [view-name:]attribute-name.

The view-name is posix.


Related Tutorials