集册 Java实例教程 使用Files类的setOwner方法

使用Files类的setOwner方法

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

541
使用Files类的setOwner方法

import java.io.IOException;

import java.nio.file.FileSystems;

import java.nio.file.Files;

import java.nio.file.Path;
/**
 * n o w j a v a . c o m 提 供 
**/

import java.nio.file.Paths;

import java.nio.file.attribute.FileOwnerAttributeView;

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("C:/home/docs/users.txt");

    try {

      UserPrincipalLookupService lookupService = FileSystems.getDefault()

          .getUserPrincipalLookupService();


      UserPrincipal userPrincipal = lookupService

          .lookupPrincipalByName("jennifer");
/* 来自 N o w  J a v a  . c o m*/

      Files.setOwner(path, userPrincipal);

      

      FileOwnerAttributeView view = Files.getFileAttributeView(path,

          FileOwnerAttributeView.
展开阅读全文