這篇文章給大家分享的是有關(guān)使用java修改文件所有者及其權(quán)限的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)建站專(zhuān)注為客戶(hù)提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、陸川網(wǎng)絡(luò)推廣、重慶小程序開(kāi)發(fā)、陸川網(wǎng)絡(luò)營(yíng)銷(xiāo)、陸川企業(yè)策劃、陸川品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪(fǎng)、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供陸川建站搭建服務(wù),24小時(shí)服務(wù)熱線(xiàn):13518219792,官方網(wǎng)址:www.cdcxhl.com這篇文章主要介紹了如何使用java修改文件所有者及其權(quán)限,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.設(shè)置所有者
管理文件所有者
Files.getOwner()和Files.setOwner()方法
要使用UserPrincipal來(lái)管理文件的所有者
(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 = 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(); } }}
輸出
查看文件詳細(xì)信息
2.ACL文件權(quán)限
Windows上支持ACL類(lèi)型文件屬性
使用AclFileAttributeView的
getAcl()方法獲取文件的AclEntry列表
setAcl()方法設(shè)置文件的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 { ListaclEntries = 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(); } }}
輸出結(jié)果為
Principal: BUILTIN\Administrators (Alias)Type: ALLOWPermissions 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: ALLOWPermissions 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: ALLOWPermissions 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: ALLOWPermissions are:READ_ATTRIBUTES SYNCHRONIZE EXECUTE READ_DATA READ_ACL READ_NAMED_ATTRS
(2)為指定用戶(hù)添加新的ACL條目
e:/test1.txt為用戶(hù)abc添加DATA_READ和DATA_ WRITE權(quán)限 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"); Setpermissions = 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(); } }}
輸出結(jié)果比剛才多了
Principal: hkgi-PC\abc (User)Type: ALLOWPermissions are:WRITE_DATA READ_DATA
3.POSIX文件權(quán)限
UNIX支持POSIX標(biāo)準(zhǔn)文件屬性
PosixFilePermission枚舉類(lèi)型定義九個(gè)常量,每個(gè)權(quán)限組件一個(gè)。
九個(gè)常數(shù)命名為X_Y,其中X是OWNER,GROUP和OTHERS,Y是READ,WRITE和EXECUTE。
PosixFilePermissions的toString()方法將一組PosixFilePermission枚舉常量轉(zhuǎn)換為rwxrwxrwx形式的字符串
PosixFileAttributeView的setPermissions()方法用來(lái)設(shè)置權(quán)限
(1)輸出/www/test1.txt的權(quán)限
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(); Setpermissions = 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(); } }}
輸出結(jié)果
rw-r--r--
(2)讀取和更新名為test的文件權(quán)限
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(); Setpermissions = 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(); } }}
輸出結(jié)果
old:rw-r-----Permissions set successfully.new:rwxrw----
感謝各位的閱讀!關(guān)于“使用java修改文件所有者及其權(quán)限的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!