集册 Java实例教程 管理属性和权限文件和目录

管理属性和权限文件和目录

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

521
管理属性和权限文件和目录
//来 自 时 代 J a v a 公 众 号 - nowjava.com

import java.io.IOException;

import java.nio.file.FileSystem;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.attribute.FileAttribute;

import java.nio.file.attribute.PosixFileAttributeView;

import java.nio.file.attribute.PosixFileAttributes;

import java.nio.file.attribute.PosixFilePermission;

import java.nio.file.attribute.PosixFilePermissions;

import java.util.Set;


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();/*from n o w j a v a . c o m - 时  代  Java*/

      Set<PosixFilePermission> permissions = attributes.permissions();


      for (PosixFilePermission permission : permissions) {

        System.out.print(permission.toString() + ' ');

      }

      System.out.println();


      FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions

          .asFileAttribute(permissions);

      Set<PosixFilePermission> fileAttributeSet = fileAttributes.value();

      for (PosixFilePermission posixFilePermission : fileAttributeSet) {

        System.out.print(posixFilePermission.toString() + ' ');

      }


      System.out.println();

      System.out.println(PosixFilePermissions.toStr
展开阅读全文