Set a File Owner Using Files.setAttribute() - Java File Path IO

Java examples for File Path IO:File Owner

Description

Set a File Owner Using Files.setAttribute()

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.UserPrincipal;

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

    UserPrincipal owner = null;//w  ww  . j  a v  a  2  s .  c  o m
    Path path = Paths.get("C:/folder1/folder2/folder4", "test.txt");
    try {
      owner = path.getFileSystem().getUserPrincipalLookupService().

      lookupPrincipalByName("apress");
      Files.setAttribute(path, "owner:owner", owner, LinkOption.NOFOLLOW_LINKS);
    } catch (IOException e) {
      System.err.println(e);
    }

  }
}

Result


Related Tutorials