java如何为文件及文件夹添加权限
java如何为⽂件及⽂件夹添加权限⽬录
java 为⽂件及⽂件夹添加权限
java 修改⽂件所有者及其权限
1.设置所有者
2.ACL⽂件权限
3.POSIX⽂件权限
java 为⽂件及⽂件夹添加权限
/**
* 增加权限,使路径可上传⽂件
*/
public static void addChmod777(String filePath) throws IOException {
if (!Property("os.name").startsWith("Win")) {
String cmdGrant = "chmod 777 " + filePath;
BaseLogMethod.logInfo(TAG, "File Augmentation after Moving:" + cmdGrant);
}
}
/**
* 增加权限,使路径及⼦路径都有权限
*/
public static void addRChmod777(String filePath) throws IOException {
if (!Property("os.name").startsWith("Win")) {
String cmdGrant = "chmod -R 777 " + filePath;
BaseLogMethod.logInfo(TAG, ".addRChmod777: File Augmentation after Moving:" + cmdGrant);
}
}
java 修改⽂件所有者及其权限
1.设置所有者
管理⽂件所有者
要使⽤UserPrincipal来管理⽂件的所有者
(1)更改⽂件的所有者
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 = ("/");
FileOwnerAttributeView foav = FileAttributeView(path,
FileOwnerAttributeView.class);
try {
UserPrincipal owner = Owner();
System.out.format("Original owner  of  %s  is %s%n", path,
FileSystem fs = Default();
UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();
正能量感恩语UserPrincipal newOwner = upls.lookupPrincipalByName("abc");
foav.setOwner(newOwner);
UserPrincipal changedOwner = Owner();
System.out.format("New owner  of  %s  is %s%n", path,
}catch (IOException e){
e.printStackTrace();
}
}
}
输出
查看⽂件详细信息
2.ACL⽂件权限
Windows上⽀持ACL类型⽂件属性
使⽤AclFileAttributeView的
getAcl()⽅法获取⽂件的AclEntry列表
setAcl()⽅法设置⽂件的AclEntry列表
(1)读取⽂件e:/的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 = ("e:/");
AclFileAttributeView aclView = FileAttributeView(path,
AclFileAttributeView.class);
if (aclView == null) {
System.out.format("ACL view  is not  supported.%n");
return;
}
try {
List<AclEntry> aclEntries = Acl();
for (AclEntry entry : aclEntries) {
System.out.format("Principal: %s%n", entry.principal());
System.out.format("Type: %s%n", pe());
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:/为⽤户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 = ("e:/");
AclFileAttributeView aclView = FileAttributeView(path,
AclFileAttributeView.class);
if (aclView == null) {
System.out.format("ACL view  is not  supported.%n");
return;
}
try {
UserPrincipal bRiceUser = Default()
.getUserPrincipalLookupService().lookupPrincipalByName("abc");
Set<AclEntryPermission> permissions = EnumSet.of(READ_DATA, WRITE_DATA);
AclEntry.Builder builder = wBuilder();
builder.setPrincipal(bRiceUser);
builder.setType(AclEntryType.ALLOW);
builder.setPermissions(permissions);
AclEntry newEntry = builder.build();
List<AclEntry> aclEntries = Acl();
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)输出/的权限
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 = ("/");
PosixFileAttributeView posixView = FileAttributeView(path,
PosixFileAttributeView.class);
try{鼓励孩子加油的暖心话
PosixFileAttributes attribs = adAttributes();
Set<PosixFilePermission> permissions = attribs.permissions();
// Convert the file permissions into the rwxrwxrwx string form
String rwxFormPermissions = String(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 = ("/");
PosixFileAttributeView posixView = FileAttributeView(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 = adAttributes();
Set<PosixFilePermission> permissions = attribs.permissions();
// Convert the set of posix file permissions into rwxrwxrwx form
String rwxFormPermissions = String(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----
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。