集册 Java实例教程 将文件的权限设置为

将文件的权限设置为

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

540
通过调用fromString()方法将文件的权限设置为硬编码字符串:

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;
/**
 * 时代Java 提 供 
**/

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) {


    PosixFileAttributes attr = null;

    Path path = Paths.get("/home/folder1/folder2/folder3/test.txt");

    try {

      attr = Files.getFileAttributeView(path, PosixFileAttributeView.class)

          .readAttributes();

    } catch (IOException e) {

      System.err.println(e);/** 来自 N o w  J a v a  .   c o m**/

    }

    System.out.println("File owner: " + attr.owner().getName());

    System.out.println("File group: " + attr.group().getName());

    System.out.println("File permissions: " + attr.permissions().toString());


    Path new_path = Paths.get("/home/folder1/folder2/folder3/new_test.txt");

    Set<PosixFilePermission> permissions
展开阅读全文