java中的chmod根据模式而不是字符串

我在 java 中有一个动态 int 代表模式,例如:450、777 等我需要更改现有文件权限,我尝试探索 Files.setPosixFilePermissions Api 但这需要字符串格式的模式,如 -rw-r--r--


Files.setPosixFilePermissions(

    path,

    PosixFilePermissions.fromString("-rw-r--r--")

);

但是,如果它的数字而不是像 450、777 这样的字符串在运行时可以是任何东西,我该如何实现呢?


临摹微笑
浏览 234回答 2
2回答

阿波罗的战车

您可以通过如下简单的转换来做到这一点:&nbsp; &nbsp; public class Chmod {&nbsp; &nbsp; &nbsp; &nbsp; public Set<PosixFilePermission> fromInt(int perms) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final char[] ds = Integer.toString(perms).toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final char[] ss = {'-','-','-','-','-','-','-','-','-'};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = ds.length-1; i >= 0; i--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int n = ds[i] - '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i == ds.length-1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 1) != 0) ss[8] = 'x';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 2) != 0) ss[7] = 'w';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 4) != 0) ss[6] = 'r';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (i == ds.length-2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 1) != 0) ss[5] = 'x';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 2) != 0) ss[4] = 'w';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 4) != 0) ss[3] = 'r';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (i == ds.length-3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 1) != 0) ss[2] = 'x';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 2) != 0) ss[1] = 'w';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((n & 4) != 0) ss[0] = 'r';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String sperms = new String(ss);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%d -> %s\n", perms, sperms);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return PosixFilePermissions.fromString(sperms);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Chmod test = new Chmod();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.fromInt(444);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.fromInt(1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.fromInt(777);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.fromInt(666);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.fromInt(604);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; test.fromInt(0);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕运维8079593

引用维基百科,您只需弄清楚这些数字是如何产生的:数字符号另一种表示 Unix 权限的方法是八进制(base-8)表示法,如 stat -c %a 所示。该符号至少由三位数字组成。最右边的三个数字中的每一个都代表权限的不同组成部分:所有者、组和其他。(如果存在第四位数字,则最左边(高位)数字地址三个附加属性,setuid 位、setgid 位和粘性位。)这些数字中的每一个都是二进制数字系统中其组成位的总和。结果,特定位添加到总和中,因为它由数字表示:读取位的总数加 4(二进制 100),写入位的总数加 2(二进制 010),并且执行位将其总数加 1(二进制 001)。...结束报价换句话说:您需要做一些二进制掩码来计算表达该数字所需的 r、w、x 字符。您可以手动执行此操作,请参见此处或此处。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java