如何使用java修改文件所有者及其权限

作者:慕尘 时间:2023-11-16 09:35:53 

这篇文章主要介绍了如何使用java修改文件所有者及其权限,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.设置所有者

管理文件所有者

Files.getOwner()和Files.setOwner()方法

要使用UserPrincipal来管理文件的所有者

(1)更改文件的所有者

如何使用java修改文件所有者及其权限


import java.io.IOException;
import java.nio.file.*;
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) {

Path path = Paths.get("/www/test1.txt");

FileOwnerAttributeView foav = Files.getFileAttributeView(path,
       FileOwnerAttributeView.class);
   try {
     UserPrincipal owner = foav.getOwner();
     System.out.format("Original owner of %s is %s%n", path,
         owner.getName());

FileSystem fs = FileSystems.getDefault();
     UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();

UserPrincipal newOwner = upls.lookupPrincipalByName("abc");
     foav.setOwner(newOwner);

UserPrincipal changedOwner = foav.getOwner();
     System.out.format("New owner of %s is %s%n", path,
         changedOwner.getName());

}catch (IOException e){
     e.printStackTrace();
   }

}
}

输出

如何使用java修改文件所有者及其权限

查看文件详细信息

如何使用java修改文件所有者及其权限

2.ACL文件权限

Windows上支持ACL类型文件属性

使用AclFileAttributeView的

getAcl()方法获取文件的AclEntry列表

setAcl()方法设置文件的AclEntry列表

(1)读取文件e:/test1.txt的ACL条目


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclFileAttributeView;
import java.util.List;
import java.util.Set;

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

Path path = Paths.get("e:/test1.txt");
   AclFileAttributeView aclView = Files.getFileAttributeView(path,
       AclFileAttributeView.class);
   if (aclView == null) {
     System.out.format("ACL view is not supported.%n");
     return;
   }
   try {
     List<AclEntry> aclEntries = aclView.getAcl();
     for (AclEntry entry : aclEntries) {
       System.out.format("Principal: %s%n", entry.principal());
       System.out.format("Type: %s%n", entry.type());
       System.out.format("Permissions are:%n");

Set<AclEntryPermission> permissions = entry.permissions();
       for (AclEntryPermission p : permissions) {
         System.out.format("%s %n", p);
       }

}
   } catch (IOException e) {
     e.printStackTrace();
   }

}
}

输出结果为


Principal: BUILTIN\Administrators (Alias)
Type: ALLOW
Permissions are:
WRITE_DATA
WRITE_OWNER
APPEND_DATA
SYNCHRONIZE
WRITE_ATTRIBUTES
EXECUTE
READ_DATA
DELETE_CHILD
READ_ATTRIBUTES
WRITE_NAMED_ATTRS
WRITE_ACL
DELETE
READ_ACL
READ_NAMED_ATTRS
Principal: NT AUTHORITY\SYSTEM (Well-known group)
Type: ALLOW
Permissions are:
WRITE_DATA
WRITE_OWNER
APPEND_DATA
SYNCHRONIZE
WRITE_ATTRIBUTES
EXECUTE
READ_DATA
DELETE_CHILD
READ_ATTRIBUTES
WRITE_NAMED_ATTRS
WRITE_ACL
DELETE
READ_ACL
READ_NAMED_ATTRS
Principal: NT AUTHORITY\Authenticated Users (Well-known group)
Type: ALLOW
Permissions are:
WRITE_DATA
READ_ATTRIBUTES
APPEND_DATA
WRITE_NAMED_ATTRS
SYNCHRONIZE
WRITE_ATTRIBUTES
EXECUTE
DELETE
READ_DATA
READ_ACL
READ_NAMED_ATTRS
Principal: BUILTIN\Users (Alias)
Type: ALLOW
Permissions are:
READ_ATTRIBUTES
SYNCHRONIZE
EXECUTE
READ_DATA
READ_ACL
READ_NAMED_ATTRS

(2)为指定用户添加新的ACL条目

e:/test1.txt为用户abc添加DATA_READ和DATA_ WRITE权限


import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;

import static java.nio.file.attribute.AclEntryPermission.READ_DATA;
import static java.nio.file.attribute.AclEntryPermission.WRITE_DATA;

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

Path path = Paths.get("e:/test1.txt");
   AclFileAttributeView aclView = Files.getFileAttributeView(path,
       AclFileAttributeView.class);
   if (aclView == null) {
     System.out.format("ACL view is not supported.%n");
     return;
   }
   try {

UserPrincipal bRiceUser = FileSystems.getDefault()
         .getUserPrincipalLookupService().lookupPrincipalByName("abc");

Set<AclEntryPermission> permissions = EnumSet.of(READ_DATA, WRITE_DATA);

AclEntry.Builder builder = AclEntry.newBuilder();
     builder.setPrincipal(bRiceUser);
     builder.setType(AclEntryType.ALLOW);
     builder.setPermissions(permissions);
     AclEntry newEntry = builder.build();

List<AclEntry> aclEntries = aclView.getAcl();

aclEntries.add(newEntry);

aclView.setAcl(aclEntries);
   }catch (IOException e){
     e.printStackTrace();
   }
 }
}

输出结果比刚才多了


Principal: hkgi-PC\abc (User)
Type: ALLOW
Permissions are:
WRITE_DATA
READ_DATA

3.POSIX文件权限

UNIX支持POSIX标准文件属性

PosixFilePermission枚举类型定义九个常量,每个权限组件一个。

九个常数命名为X_Y,其中X是OWNER,GROUP和OTHERS,Y是READ,WRITE和EXECUTE。

PosixFilePermissions的toString()方法将一组PosixFilePermission枚举常量转换为rwxrwxrwx形式的字符串

PosixFileAttributeView的setPermissions()方法用来设置权限

(1)输出/www/test1.txt的权限


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.Set;

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

Path path = Paths.get("/www/test1.txt");
   PosixFileAttributeView posixView = Files.getFileAttributeView(path,
       PosixFileAttributeView.class);
   try{
     PosixFileAttributes attribs = posixView.readAttributes();
     Set<PosixFilePermission> permissions = attribs.permissions();
     // Convert the file permissions into the rwxrwxrwx string form
     String rwxFormPermissions = PosixFilePermissions.toString(permissions);
     // Print the permissions
     System.out.println(rwxFormPermissions);
   }catch (IOException e){
     e.printStackTrace();
   }

}
}

输出结果

rw-r--r--

(2)读取和更新名为test的文件权限


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.*;
import java.util.EnumSet;
import java.util.Set;

import static java.nio.file.attribute.PosixFilePermission.*;

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

Path path = Paths.get("/www/test1.txt");
   PosixFileAttributeView posixView = Files.getFileAttributeView(path,
       PosixFileAttributeView.class);

if (posixView == null) {
     System.out.format("POSIX attribute view is not supported%n.");
     return;
   }
   System.out.println("old:");
   readPermissions(posixView);
   updatePermissions(posixView);
   System.out.println("new:");
   readPermissions(posixView);

}

public static void readPermissions(PosixFileAttributeView posixView) {
   try{
     PosixFileAttributes attribs;
     attribs = posixView.readAttributes();
     Set<PosixFilePermission> permissions = attribs.permissions();
     // Convert the set of posix file permissions into rwxrwxrwx form
     String rwxFormPermissions = PosixFilePermissions.toString(permissions);
     System.out.println(rwxFormPermissions);
   }catch (IOException e){
     e.printStackTrace();
   }

}
 public static void updatePermissions(PosixFileAttributeView posixView) {
   try {
     Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE,OWNER_EXECUTE,
         GROUP_READ,GROUP_WRITE);
     posixView.setPermissions(permissions);
     System.out.println("Permissions set successfully.");
   }catch (IOException e){
     e.printStackTrace();
   }
 }
}

输出结果


old:
rw-r-----
Permissions set successfully.
new:
rwxrw----

来源:https://www.cnblogs.com/baby123/p/11122289.html

标签:java,修改,文件,所有者,权限
0
投稿

猜你喜欢

  • Spring Cloud Gateway替代zuul作为API网关的方法

    2023-05-03 07:19:58
  • Java 实战项目之在线点餐系统的实现流程

    2022-08-05 04:11:35
  • Java基于ShardingSphere实现分库分表的实例详解

    2022-04-20 18:43:37
  • android中DatePicker和TimePicker的使用方法详解

    2021-08-28 14:45:13
  • Jmeter post上传文件实现过程详解

    2022-01-09 14:02:28
  • c#方法重写和隐藏的学习示例

    2022-12-24 14:20:12
  • 详解Dagger2在Android开发中的新用法

    2021-08-23 22:39:40
  • c#使用windows服务更新站点地图的详细示例

    2021-07-24 10:45:52
  • WPF InkCanvas基本操作方法详解

    2023-07-29 06:26:59
  • C++实现简单酒店管理系统

    2021-07-31 13:52:40
  • 如何解决Mybatis--java.lang.IllegalArgumentException: Result Maps collection already contains value for X

    2022-03-20 10:35:44
  • Java异常处理操作 Throwable、Exception、Error

    2022-02-19 20:56:13
  • SpringBoot集成EasyExcel的应用场景分析

    2023-01-09 07:47:45
  • 浅谈@RequestMapping注解的注意点

    2023-07-11 02:12:39
  • MFC程序设计常用技巧汇总

    2023-11-02 20:37:12
  • spring-boot整合ehcache实现缓存机制的方法

    2023-06-18 20:05:10
  • Android 实现手机接通电话后振动提示的功能

    2022-01-10 23:56:58
  • Android开发中应用程序分享功能实例

    2021-05-27 20:38:46
  • 利用Java写一个学生管理系统

    2023-09-24 17:06:54
  • jbuilder2006连接sqlserver2000的方法

    2022-08-21 14:20:09
  • asp之家 软件编程 m.aspxhome.com