Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add permissions to files and folders by java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how java adds permissions to files and folders. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Java add permissions to files and folders / * add permissions to enable the path to upload files * / public static void addChmod777 (String filePath) throws IOException {if (! System.getProperty ("os.name"). StartsWith ("Win")) {String cmdGrant = "chmod 777" + filePath; BaseLogMethod.logInfo (TAG, "File Augmentation after Moving:" + cmdGrant) Runtime.getRuntime () .exec (cmdGrant);}} / * add permissions to both paths and subpaths * / public static void addRChmod777 (String filePath) throws IOException {if (! System.getProperty ("os.name") .startsWith ("Win")) {String cmdGrant = "chmod-R 777" + filePath BaseLogMethod.logInfo (TAG, ".addRChmod777: File Augmentation after Moving:" + cmdGrant); Runtime.getRuntime () .exec (cmdGrant);}} java modifies the file owner and its permissions 1. Set owner

Manage file owner

Files.getOwner () and Files.setOwner () methods

To use UserPrincipal to manage the owner of the file

(1) change the owner of the file

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 ();}

Output

View file details

2.ACL file permissions

ACL type file attributes are supported on Windows

Using AclFileAttributeView's

The getAcl () method gets the AclEntry list of files

The setAcl () method sets the AclEntry list of files

(1) read the ACL entry of the file e:/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.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 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 permissions = entry.permissions () For (AclEntryPermission p: permissions) {System.out.format ("% s% n", p);} catch (IOException e) {e.printStackTrace ();}

The output is

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) add a new ACL entry for the specified user

E:/test1.txt adds DATA_READ and DATA_ WRITE permissions to user abc

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 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 aclEntries = aclView.getAcl (); aclEntries.add (newEntry); aclView.setAcl (aclEntries);} catch (IOException e) {e.printStackTrace ();}}

The output is more than just now.

Principal: hkgi-PC\ abc (User)

Type: ALLOW

Permissions are:

WRITE_DATA

READ_DATA

3.POSIX file permissions

UNIX supports POSIX standard file attributes

The PosixFilePermission enumeration type defines nine constants, one for each permission component.

Nine constants are named Xray, where X is OWNER,GROUP and OTHERS,Y is READ,WRITE and EXECUTE.

The toString () method of PosixFilePermissions converts a set of PosixFilePermission enumeration constants to a string in rwxrwxrwx form

The setPermissions () method of PosixFileAttributeView is used to set permissions

(1) permission to export / 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 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 ();}

Output result

Rw-r--r--

(2) read and update file permissions named 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 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 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 ();}

Output result

Old:

Rw-r-

Permissions set successfully.

New:

Rwxrw

This is the end of the article on "how java adds permissions to files and folders". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report