集册 Java实例教程 修改文件的POSIX所有权

修改文件的POSIX所有权

欢马劈雪     最近更新时间:2020-01-02 10:19:05

491
修改文件的POSIX所有权

import java.io.IOException;

import java.nio.file.FileSystems;

import java.nio.file.Files;// from NowJava.com

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.GroupPrincipal;

import java.nio.file.attribute.PosixFileAttributeView;

import java.nio.file.attribute.PosixFileAttributes;

import java.nio.file.attribute.UserPrincipal;

import java.nio.file.attribute.UserPrincipalLookupService;


public class Main {

  public static void main(String[] args) throws Exception {

    Path path = Paths.get("home/docs/users.txt");

    try {

      PosixFileAttributeView view = Files.getFileAttributeView(path,

          PosixFileAttributeView.class);


      PosixFileAttributes attributes = view.readAttributes();/* 来自 时代Java*/


      System.out.println("Old Group: " + attributes.group().getName());

      System.out.println("Old Owner: " + attributes.owner().getName());

      System.out.println();


      UserPrincipalLookupService lookupService = FileSystems.getDefault()

          .getUserPrincipalLookupService();

      UserPrincipal userPrincipal = lookupService

          .lookupPrincipalByName("jennifer");

      GroupPrincipal groupPrincipal = lookupService

          .lookupPrincipalByGroupName("jennifer");

      view.setGroup(groupPrincipal);

      view.setOwner(userPrincipal);


      attributes =
展开阅读全文